Introduction:

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’.

Definition:

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.

Final Class in SAP ABAP

Final Class in SAP ABAP

Now, if class ‘ELLIPSE’ would be defined as ‘Final’, then this inheritance would not be possible.

Program:

Below is the sample program:

  1. CLASS_1 declared as ‘FINAL’ and hence giving error while inheriting CLASS_2 from CLASS_1.
    ABAP Final Class Example 1

    ABAP Final Class Example 1

  2. In second example, one of the methods of a class is defined as ‘Final’. The subclass is not having an implementation of final method.
    ABAP Final Class Example 2

    ABAP Final Class Example 2

Advantages:

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.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Translate »