Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

Can we make class final in java if yes then what is the effect of that?

user-image
Question ajoutée par Ali Hasnain , Lead Developer (Remote) , Sprinting Software Denmark
Date de publication: 2017/05/12
Thasleem TS
par Thasleem TS , Web Designer , ZAK Designers

Yes we can create class as final. when the class is in final the properties of the classs cant accessed to outside. 

Nasli Minhaj
par Nasli Minhaj , Software Engineer , retailcloud

Yes we can declare a class as final .Once it became a final class it can not be extended/inherited .Doing this can confer security and efficiency benefits,so many of the Java standadrd library classes are final ,such as System,String etc.

Yes,If we mark class as final then it can't be extended.

It is blocking the inheritance characteristic by limiting OOPS behaviour.In some cases if we want the class to behave as utility class like wrapper classes(String, Maths,..), we can mark the class as final.

Shoaib Bazmi
par Shoaib Bazmi , Senior Developer , Adlib Solutions

Yes, you can declare an entire class final. A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like the String class.

You make a class immutable like this:

public final class Immutable{ private final String name; public Immutable(String name){ this.name = name; } public String getName() { return this.name; } }

Immutable classes are useful because they're thread-safe. They also express something deep about your design: "Can't change this." When it applies, it's exactly what you need.

 

More Questions Like This