ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل Ali Hasnain , Lead Developer (Remote) , Sprinting Software Denmark
تاريخ النشر: 2017/05/12
Thasleem TS
من قبل 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
من قبل 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.

Senthilkumar Murugesan
من قبل Senthilkumar Murugesan , Technical Lead , HCL TECHNOLOGIES LTD

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
من قبل 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.

 

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟