Communiquez avec les autres et partagez vos connaissances professionnelles

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

Suivre

What is the use of (final class) when you apply OOP, plus advantages and disadvantages?

user-image
Question ajoutée par Feras Abualrub , Web Solutions Manager , Qistas for Information Technology
Date de publication: 2013/09/06
Utilisateur supprimé
par Utilisateur supprimé

A final class means that this class cannot be inherited by another one,for example the following code is not correct and will result in a fatal error ..

final class A {

........

}

class B extends A {

......

}

You can use it for example if you are developing an API and you dont want the users to override the functionality of  certain functionalties ...

Basically using it deprives other users from the freedom of using your code , however its optional  depending on the way you are structuring your code... so I think here there is not much of a room for talking about pros or cons... 

Imranullah Khan
par Imranullah Khan , Full Stack Developer , GoDigital Inc

We make class final when a class behaviour need not to be modified any more. The functionality of that class is final. You can see we can not inherit from a final class becuase by inheriting we intend to change some behaviour of a class within another class but as final class has complete and final functionality so we cant inherit from it. Some example of final classes are String, Integer, Double and other wrapper classes. Like this methods can be final too. In JDK an example of a final method is toString() 

Sayed Huzaifa
par Sayed Huzaifa , Devops Lead , Telus

object-oriented programming (OOP), inheritance is a way to establish Is-a relationships between objects. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classessuperclasses, or parent classes. The resulting classes are known as derived classessubclasses, or child classes.

More Questions Like This