Inheritance is used to derive a new class from an existing class. New class is called child class, subclass or derived class. Existing class is called parent class or superclass.
- The subclass inherits all the components of the superclass defined under PUBLIC and PROTECTED section, but not under PRIVATE section.
- Subclass components can access the superclass components defined under PUBLIC and PROTECTED section.
- The Subclass object can access components of both superclass and subclass defined under PUBLIC section only.
- Subclass can add their own components as well as change the implementation of the inherited methods (method redefinition).
- A Subclass can have only one Super Class. In other words, ABAP supports single inheritance. Multiple inheritance is not supported in ABAP Objects.
- The subclass is aware of its superclass, but the superclass is not aware who its subclasses are.
- The inheritance tree can be any number of levels.
Syntax for Inheritance
CLASS <subclass> DEFINITION INHERITING FROM <superclass>.
...
ENDCLASS.
Inheritance Example
Output
If you are wondering, can we access the static components of the superclass by using the subclass class-name. The answer is YES, you can access the static components defined under PUBLIC section of the superclass by using the class name of the subclass.
ABAP Objects Inheritance Tree
In object-oriented ABAP, multiple inheritance is not possible. ABAP Object supports only single inheritance.
Although, a subclass can have only one superclass, it can inherit any number of Interfaces. Thus, multiple inheritance is supported in the form on Interfaces but not directly with a class.