SAP ABAP : OOPS Concept - Inheritance

Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the mechanism in ABAP by which one class is allowed to inherit the features(fields and methods) of another class. 

Important terminology: 

  • Super Class: The class whose features are inherited is known as superclass(or a base class or a parent class).
  • Sub Class: The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
  • Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

How to use inheritance in ABAP

The keyword used for inheritance is INHERITING FROM.

Example :


REPORT ztest_demo_sup.
class lcl_animal DEFINITION"class declaration
  PUBLIC SECTION"access modifier
   DATA name TYPE char10."attributes
   DATA color TYPE char10.
   CLASS-METHODS class_constructor.
   METHODS constructor IMPORTING name1 TYPE char10.
   METHODS m_eat "behaviours instance method
   METHODSm_bark.
ENDCLASS.
CLASS lcl_pets  DEFINITION INHERITING FROM lcl_animal.
  PUBLIC SECTION.
  DATA pet_name TYPE char10.
  METHODS constructor IMPORTING name TYPE char10.
  METHODS m_eat REDEFINITION.
ENDCLASS.
DATA lv_dog TYPE REF TO lcl_animal"declaring object of class

START-OF-SELECTION.
*"there is two way to create object of class after release 7.4
*CREATE OBJECT lv_dog EXPORTING name1 = 'Milo'. "or
*
*DATA(lv_cat) = NEW lcl_animal( name1 = 'Kitty')."incline declaration with New keyword "supported only release after 7.4 ve
DATA(lv_snakeNEW lcl_petsname 'Cobra' ).
*lv_dog->m_eat( ).
*lv_cat->m_eat( )."to access instance method we have to create the object of the class

lv_snake->m_eat)."child class

lv_snake->m_bark)

" We can directly access the super class method by using the subclass objects 


CLASS lcl_animal IMPLEMENTATION.
  METHOD class_constructor.
*    WRITE : / ' Super hello'.
  ENDMETHOD.
  METHOD constructor.
    name name1.
    color 'red'.
    WRITE / name.
  ENDMETHOD.
  METHOD m_eat.
    WRITE / name ,'is eating'.

  ENDMETHOD.
  METHOD m_bark.
*    WRITE : / name ,'is barking'.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_pets IMPLEMENTATION.
  METHOD constructor.
    super->constructorname1 name ).
    pet_name name.
  ENDMETHOD.
  METHOD m_eat.
*    super->m_eat( ).
    WRITE / name ,'has'color' color '.
  ENDMETHOD.
ENDCLASS.


In the above program, when an object lv_snake of lcl_pets class is created, a copy of all methods and fields of the superclass acquire memory in this object. That is why by using the object of the subclass we can also access the members of a superclass. 

Please note that during inheritance only the object of the subclass is created, not the superclass.


                            Types of Inheritance in ABAP


Below are the different types of inheritance which are supported by ABAP. 

1. Single Inheritance: 

In single inheritance, subclasses inherit the features of one superclass. In the image below, class A serves as a base class for the derived class B


Example : You can refer above example .

2. Multilevel Inheritance:

In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In the below image, class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C.



Key Points :

In ABAP, a class cannot directly access the grandparent’s members.


REPORT ztest_demo_sup.
class lcl_animal DEFINITION"class declaration
  PUBLIC SECTION"access modifier
   DATA name TYPE char10."attributes

   CLASS-METHODS class_constructor.
   METHODS constructor IMPORTING name1 TYPE char10.
   METHODS m_eat "behaviours instance method


   PROTECTED SECTION.
   DATA color TYPE char10.
      METHODSm_bark.
ENDCLASS.

CLASS lcl_pets  DEFINITION INHERITING FROM lcl_animal.
  PUBLIC SECTION.
  DATA pet_name TYPE char10.
  METHODS constructor IMPORTING name TYPE char10.
  METHODS m_eat REDEFINITION.
ENDCLASS.

CLASS lcl_birds DEFINITION INHERITING FROM lcl_pets.
  PUBLIC SECTION.
  DATA bird_name TYPE char10.
  METHODS constructor IMPORTING name TYPE char10.
  METHODS m_eat REDEFINITION.
ENDCLASS.
DATA lv_dog TYPE REF TO lcl_animal"declaring object of class

START-OF-SELECTION.
DATA(lv_birdNEW lcl_birdsname 'Peacock' ).


lv_bird->m_eat).
CLASS lcl_animal IMPLEMENTATION.
  METHOD class_constructor.
  ENDMETHOD.
  METHOD constructor.
    name name1.
    color 'red'.
  ENDMETHOD.
  METHOD m_eat.
    WRITE / name ,'Grandp is eating'.

  ENDMETHOD.
  METHOD m_bark.
    WRITE / name ,'Grandp is barking'.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_pets IMPLEMENTATION.
  METHOD constructor.
    super->constructorname1 name ).
    pet_name name.
  ENDMETHOD.
  METHOD m_eat.
*    super->m_eat( ).
    WRITE 'Parent is eating'.
    WRITE / name ,'has'color' color '.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_birds IMPLEMENTATION.
    METHOD constructor.
    super->constructorname name ).
    bird_name name.
  ENDMETHOD.
  METHOD m_eat.
    super->m_eat).
    WRITE'Grandchild is eating'.
    WRITE / name ,'has'color' color '.
  ENDMETHOD.
ENDCLASS.



OutPut will come :

Parent is eating

Peacock    has red         color

Grandchild is eating

Peacock    has red         color


We are not able to access grandparent method m_eat. To access that we have to implement our method like this .

Example :


REPORT ztest_demo_sup.
class lcl_animal DEFINITION"class declaration
  PUBLIC SECTION"access modifier
   DATA name TYPE char10."attributes

   CLASS-METHODS class_constructor.
   METHODS constructor IMPORTING name1 TYPE char10.
   METHODS m_eat "behaviours instance method


   PROTECTED SECTION.
   DATA color TYPE char10.
      METHODSm_bark.
ENDCLASS.

CLASS lcl_pets  DEFINITION INHERITING FROM lcl_animal.
  PUBLIC SECTION.
  DATA pet_name TYPE char10.
  METHODS constructor IMPORTING name TYPE char10.
  METHODS m_eat REDEFINITION.
ENDCLASS.

CLASS lcl_birds DEFINITION INHERITING FROM lcl_pets.
  PUBLIC SECTION.
  DATA bird_name TYPE char10.
  METHODS constructor IMPORTING name TYPE char10.
  METHODS m_eat REDEFINITION.
ENDCLASS.
DATA lv_dog TYPE REF TO lcl_animal"declaring object of class

START-OF-SELECTION.
DATA(lv_birdNEW lcl_birdsname 'Peacock' ).


lv_bird->m_eat).
CLASS lcl_animal IMPLEMENTATION.
  METHOD class_constructor.
  ENDMETHOD.
  METHOD constructor.
    name name1.
    color 'red'.
  ENDMETHOD.
  METHOD m_eat.
    WRITE / name ,'Grandp is eating'.

  ENDMETHOD.
  METHOD m_bark.
    WRITE / name ,'Grandp is barking'.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_pets IMPLEMENTATION.
  METHOD constructor.
    super->constructorname1 name ).
    pet_name name.
  ENDMETHOD.
  METHOD m_eat.
    super->m_eat).
    WRITE 'Parent is eating'.
    WRITE / name ,'has'color' color '.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_birds IMPLEMENTATION.
    METHOD constructor.
    super->constructorname name ).
    bird_name name.
  ENDMETHOD.
  METHOD m_eat.
    super->m_eat).
    WRITE'Grandchild is eating'.
    WRITE / name ,'has'color' color '.
  ENDMETHOD.
ENDCLASS.


OutPut :

Peacock    Grandp is eating

Parent is eating

Peacock    has red         color

Grandchild is eating

Peacock    has red         color


Why doesn’t ABAP allow accessing grandparent’s methods? 

It violates encapsulation. You shouldn’t be able to bypass the parent class’s behavior. It makes sense to sometimes be able to bypass your own class’s behavior (particularly from within the same method) but not your parent’s.


3. Hierarchical Inheritance: 

In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. In the below image, class A serves as a base class for the derived class B, C and D.


Example :

REPORT ztest_demo_sup.
class lcl_animal DEFINITION"class declaration
  PUBLIC SECTION"access modifier
   DATA name TYPE char10."attributes

   CLASS-METHODS class_constructor.
   METHODS constructor IMPORTING name1 TYPE char10.
   METHODS m_eat "behaviours instance method


   PROTECTED SECTION.
   DATA color TYPE char10.
      METHODSm_bark.
ENDCLASS.

CLASS lcl_pets  DEFINITION INHERITING FROM lcl_animal.
  PUBLIC SECTION.
  DATA pet_name TYPE char10.
  METHODS constructor IMPORTING name TYPE char10.
  METHODS m_eat REDEFINITION.
ENDCLASS.

CLASS lcl_birds DEFINITION INHERITING FROM lcl_animal.
  PUBLIC SECTION.
  DATA bird_name TYPE char10.
  METHODS constructor IMPORTING name TYPE char10.
  METHODS m_eat REDEFINITION.
ENDCLASS.
DATA lv_dog TYPE REF TO lcl_animal"declaring object of class

START-OF-SELECTION.
DATA(lv_birdNEW lcl_birdsname 'Peacock' ).
DATA(lv_petNEW lcl_birdsname 'Cat' ).

lv_pet->m_eat).
lv_bird->m_eat).
CLASS lcl_animal IMPLEMENTATION.
  METHOD class_constructor.
  ENDMETHOD.
  METHOD constructor.
    name name1.
    color 'red'.
  ENDMETHOD.
  METHOD m_eat.
    WRITE / name ,'Grandp is eating'.

  ENDMETHOD.
  METHOD m_bark.
    WRITE / name ,'Grandp is barking'.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_pets IMPLEMENTATION.
  METHOD constructor.
    super->constructorname1 name ).
    pet_name name.
  ENDMETHOD.
  METHOD m_eat.
    super->m_eat).
*    WRITE : / 'Parent is eating'.
    WRITE / name ,'has'color' color '.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_birds IMPLEMENTATION.
    METHOD constructor.
    super->constructorname1 name ).
    bird_name name.
  ENDMETHOD.
  METHOD m_eat.
    super->m_eat).
*    WRITE: / 'Grandchild is eating'.
    WRITE / name ,'has'color' color '.
  ENDMETHOD.
ENDCLASS.

4. Multiple Inheritance (Through Interfaces):

In Multiple inheritances, one class can have more than one superclass and inherit features from all parent classes. Please note that ABAP does not support multiple inheritances with classes.
In ABAP, we can achieve multiple inheritances only through Interfaces. In the image below, Class C is derived from interface A and B.
Example :

REPORT ztest_demo_sup.
INTERFACE if_animal.
  METHODSm_eat.
ENDINTERFACE.
INTERFACE if_bird.
  METHODSm_color.
ENDINTERFACE.

CLASS lcl_an DEFINITION.
  PUBLIC SECTION.
  INTERFACES if_animal,if_bird.
ENDCLASS.
START-OF-SELECTION.
DATA(lv_refNEW lcl_an).

lv_ref->if_animal~m_eat).
lv_ref->if_bird~m_color).
CLASS lcl_an IMPLEMENTATION.
  METHOD if_animal~m_eat.
    WRITE:  'Eating'.
  ENDMETHOD.
  METHOD if_bird~m_color.
    WRITE 'Red'.
  ENDMETHOD.
ENDCLASS.

5. Hybrid Inheritance(Through Interfaces): 
It is a mix of two or more of the above types of inheritance. Since ABAP doesn’t support multiple inheritances with classes, hybrid inheritance is also not possible with classes. In ABAP, we can achieve hybrid inheritance only through interfaces .


Key Points :

Important facts about inheritance in ABAP 

  • Default superclass: Except Object class, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of the Object class.
  • Superclass can only be one: A superclass can have any number of subclasses. But a subclass can have only one superclass. This is because ABAP does not support multiple inheritances with classes. Although with interfaces, multiple inheritances are supported by ABAP.
  • Inheriting Constructors: A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass with super keyword.
  • Private member inheritance: A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods(like getters and setters) for accessing its private fields, these can also be used by the subclass.

What all can be done in a Subclass?

In sub-classes we can inherit members as is, replace them, hide them, or supplement them with new members: 

  • The inherited fields can be used directly, just like any other fields.
  • We can declare new fields in the subclass that are not in the superclass.
  • The inherited methods can be used directly as they are.
  • We can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it .
  • We can not write a new static method in the subclass that has the same signature as the one in the superclass.
  • We can declare new methods in the subclass that are not in the superclass.
  • We can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.















Comments

Popular posts from this blog

SAP ABAP : OOPS Concept - Classes And Objects

SAP ABAP : OOPS Concept - Method