Summary -

In this topic, we described about the below sections -

Inheritance is one of the most important object-oriented programming concept. Inheritance allows to derive a new class from an existing class. Inheritance allows user to define the new class with the characteristics of the existing class which is already defined.

The existing class called as base class or super class. The new class called as subclass or derived class. Inheritance is implementing relationship between classes to inherit all components of base class to subclass.

In a subclass, we can define additional components or redefine instance methods that were inherited from the superclass. The inherited methods from super class can't be removed in the subclass. But the inherited methods from super class can be overridden in subclass. Subclass inherits the properties or methods from base class. So that we can add additional features to an existing class without modifying it.

Only the public and protected components of the superclass are visible in the subclass. The private components of the superclass exist in the subclass, but they are not visible.

Super class has no knowledge about its subclasses. However, if any changes to the super class automatically applies changes all its subclasses. Inheritance used to implement relationships between classes that are described in terms of generalization and specialization.

The basic advantage of inheritance property is reusability. To enable inheritance, use INHERITING FROM statement during class definition statement in objects. The syntax for the same is -

CLASS <sub-class> DEFINITION INHERITING FROM <super-class>.

  • <sub-class> - Specifies subclass or derived class.
  • <super-class> - Specifies super class or base class.

ABAP supports single inheritance. Let us see what is single inheritance in detail.

Single inheritance -

Single inheritance is the subclass only have one direct super class. ABAP Objects supports single inheritance. Single inheritance rule is enforced by INHERITING FROM clause that specifies the name of superclass.

Note! If any superclass is a direct subclass of another super class, the middle class can never be a subclass or a superclass due to multiple steps of inheriting.

Each super class can have many subclasses. But subclass should have only one super class.

Note! ABAP Objects does not support multiple inheritance due to the design goals for ABAP Objects was to make it as simple as possible.

Component Access Control -

A sub class can access all non-private components of its base class. i.e., Only the public and protected components of the superclass are visible in the subclass.

Below table describes about accessing various components from various places -

Accessing fromPrivate componentProtected componentPublic component
Same classYesYesYes
Derived classNoYesYes
Outside classNoNoYes

Inheritance Types -

Inheritance is basically three types and those are -

Inheritance Types Description
Public Inheritance All non-private components inherited from super class. The public components of the superclass are inherited as public components to the subclass. The protected components of the superclass are inherited as protected components to the subclass. Super class's private members are never accessible directly from a sub class, but can be accessed through calls to the super class public and protected components.
Protected Inheritance All the super class public and protected components inherited as protected components to the subclass.
Private Inheritance All the super class public and protected components inherited as private components to the subclass.

Example -

Simple example on how the inheritance implemented in the application program.


Code -

*&---------------------------------------------------------------------*
*& Report  Z_INHERITANCE
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_INHERITANCE.

* Parent Class definition
CLASS parentclass DEFINITION.
PUBLIC SECTION.

* Declaring a public variable
  DATA pub_var(35) TYPE C VALUE 'Parent class public variable'.
  
* Defining public method of parent class.
  METHODS parentmethod.
  
PROTECTED SECTION.

* Declaring a protected variable pro_var
  DATA pro_var(35) TYPE C VALUE 'Parent class protected variable'.
  
PRIVATE SECTION.

* Declaring a private variable pri_var
  DATA pri_var(35) TYPE C VALUE 'Parent class private variable'.
ENDCLASS.

* Parent Class implementation
CLASS parentclass IMPLEMENTATION.

* Method implementation
  METHOD parentmethod.
     WRITE:/ pub_var.
     WRITE:/ pro_var.
     WRITE:/ pri_var.
  ENDMETHOD.
ENDCLASS.

* Child Class Definition
CLASS Childclass DEFINITION INHERITING FROM Parentclass.
PUBLIC SECTION.

* Method definition
  METHODS childmethod.
ENDCLASS.

* Child Class implementation
CLASS childclass IMPLEMENTATION.

* Child class implementation
  METHOD childmethod.
     WRITE:/ pub_var.
     WRITE:/ pro_var.
  ENDMETHOD.
ENDCLASS.


START-OF-SELECTION.

* Declaring an object of parent class
  DATA: parentobj TYPE REF TO parentclass.
  
* Creating parent class object
  CREATE OBJECT parentobj.
  
  WRITE /'Accessing components through parent class object...'.
  SKIP 2.
* Calling parent method
  CALL METHOD: parentobj->parentmethod.
  ULINE.

* Declaring child object 
  DATA: childobj TYPE REF TO childclass.
  
* Creating child object
  CREATE OBJECT childobj.
  
  WRITE /'Accessing components through child class object...'.
  SKIP 2.

* Calling child method
  CALL METHOD: childobj->childmethod.
  ULINE.

Output -

Inheritance Example Output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

parentclass is the super class defined with three variables: pub_var (defined in public section), pro_var (defined in protected section) and pri_var (defined in private section).

Childclass is the subclass inherited from parent class. So the pub_var and pro_var can be accessed in the child class. pri_var can’t be accessed in child class as it is defined in private section and can’t be accessed outside of the parent class.