A class is said to be a ‘Final Class’ when it is no longer available for inheritance. Therefore, we won’t be able to inherit the properties or behavior of a final class. A final class does not have a subclass.
However, if you don’t want to make a whole class final, and if you want to restrict some of the methods from getting redefined, then there is an option available to declare some of the methods of a class as ‘final’.
If a class cannot be inherited further, then it is a Final Class. It is up to the developer if he wants to make a class ‘final’ or some of the methods ‘final’.
Example: Let’s take an example of this ellipse. Say, ‘ELLIPSE’ is your class. A circle inside this class is your subclass.
This is the first case, where the class ‘ELLIPSE’ is not declared as ‘final’. Therefore, a subclass ‘CIRCLE’ inherits all the components of the superclass, where the visibility section is public & protected.
Note: If there is an interface implemented in the superclass, it will get reflected in the subclass as well.
Now, if class ‘ELLIPSE’ would be defined as ‘Final’, then this inheritance would not be possible.
Below is the sample program:
- CLASS_1 declared as ‘FINAL’ and hence giving error while inheriting CLASS_2 from CLASS_1.
- In second example, one of the methods of a class is defined as ‘Final’. The subclass is not having an implementation of final method.
Inheritance is one of the powerful pillars of OOABAP but not every time, this may lead to weakens encapsulation, can create a bug hazard. To prevent this, sometimes a class is declared as ‘Final’.
Disclaimer: All the images here are taken from Google, and the Admin has no possession over it.