Register now or log in to join your professional community.
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...
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()
n 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 classes, superclasses, or parent classes. The resulting classes are known as derived classes, subclasses, or child classes.