Inheritance Concept in ABAP OOPS with Example

Inheritance Concept in ABAP OOPS with Example

Inheritance

Child Class inheriting the properties of Parent class is known as Inheritance. In ABAP, a subclass can be created for existing class using the concept of inheritance. The existing class is known as Super class

The advantage of inheritance is that the methods and the attributes of Super class need not be defined or implemented again in subclass and also additional features can be added in the new class.

Important points 

  • ABAP objects supports only single inheritance and not multiple inheritance.
  • Subclass can access public/protected components of a superclass or parent class.
  • Subclass can inherit and reimplement the public and protected methods defined in super class.
  • Final Class cannot have any subclass which means it cannot be inherited.
  • Final Methods cannot be redefined in subclass.

Syntax

CLASS <SUBCLASS-NAME> DEFINITION INHERITING FROM <SUPERCLASS-NAME>.


Generalized Class and Specialized Class

In inheritance the subclass derive the properties of parent class like attributes and methods and also new functionality specific to sub class can be added.

For example consider a class Bank Account. For example the Bank Account has two subclasses Savings and Current.

Bank Account has common/general attributes Account Holder, Balance while subclasses Savings and Current more specific properties like interest rate and calculation.

As the parent class has the common properties of sub class it is known as Generalized class and this concept is known as Generalization. This is a Bottom up approach

Identifying the subsets of parent class based on some distinguishing characteristics are known as Specialization and the subclass involved in this known as Specialized class. This is a top down approach.

Inheritance Example

The below program demonstrates the example of Inheritance.

The parent class has methods METH1 and METH2 which are inherited by child class.

The method METH2 is redefined in child class. The process of overriding the parent class method in subclass is known is known as “Method Overriding”. ABAP doesn't support method overloading.

METH3 is an additional method specific to child class. Super keyword is used to call the previous implementation of same method.

We cannot call child class methods from parent class reference. But the parent class methods can be called from child class as it has inherited the parent class properties.

Code :

REPORT ztest_program.

*-- Definition
CLASS lcl_parent DEFINITION.                            " Generalized Class
  PUBLIC SECTION.
    METHODS: meth1,
      meth2.
ENDCLASS.

CLASS lcl_child DEFINITION INHERITING FROM lcl_parent. " Specialized Class
  PUBLIC SECTION.
    METHODS meth2 REDEFINITION.
    METHODS meth3.
ENDCLASS.

*-- Implementation
CLASS lcl_parent IMPLEMENTATION.
  METHOD meth1.
    WRITE:/ 'Method 1 in Parent Class'.
  ENDMETHOD.

  METHOD meth2.
    WRITE:/ 'Method 2 in Parent Class'.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_child IMPLEMENTATION.
  METHOD meth2.
    WRITE:/ 'Method 2 Reimplemented in Child Class'.
    super->meth2( ). " Call METH2 from parent class using Super
  ENDMETHOD.
  METHOD meth3.
    WRITE:/ 'Method 3 in Child Class'.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

  DATA(lo_parent) = NEW lcl_parent( ).
  WRITE:/ '<--Parent Class Object-->'.
  lo_parent->meth1( ).
  lo_parent->meth2( ).
*  lo_parent->meth3( ). " Compilation Error - METH3 doesn't exist

  DATA(lo_child)  = NEW lcl_child( ).
  WRITE:/ '<--Child Class Object-->'.
  lo_child->meth1( ). " Child class inherits meth1 from parent
  lo_child->meth2( ). " meth2 implementation in parent overided by child class
  lo_child->meth3( ). " meth3 implemented in child class

Output:

< Parent Class Object >
Method 1 in Parent Class
Method 2 in Parent Class
< Child Class Object >
Method 1 in Parent Class
Method 2 Reimplemented in Child Class
Method 2 in Parent Class
Method 3 in Child Class

Post a Comment

Previous Post Next Post