SAP ABAP OOPS – Inheritance, Encapsulation, Polymorphism

FREE Online Courses: Click for Success, Learn for Free - Start Now!

In this tutorial, we’ll be getting deeper into the world of SAP ABAP object-oriented programming. In part 1 of the tutorial, we learnt the basic concepts of SAP ABAP objects and classes in detail. Here, we will learn about the accompanying features of object orientation in ABAP.

Inheritance in SAP ABAP

  • Inheritance means to derive code functionality from one class to another
  • It means defining a class in terms of another (parent) class
  • This feature of object orientation promotes reusability of classes
  • We can use inheritance while writing a class with which to derive a parent class, by mentioning the name of parent class in the definition of derived class
  • Hence, the class that is inherited is called parent or base class or super class, and the class inheriting the base class is the child class or derived class or subclass
  • Through inheritance, an object from derived class can obtain characteristics of objects from base class
  • The keyword to use for inheritance is ‘INHERITING FROM’, just beside the class definition

SYNTAX FOR DEFINING CLASS:

CLASS <subclass_name> DEFINITION INHERITING FROM <superclass_name>

EXAMPLE

CLASS Z_Dog DEFINITION INHERITING FROM Z_Animal

ABAP PROGRAM EXAMPLE

Report ZDATAFLAIR_INHERITANCE. 
CLASS DataflairParent Definition. 
PUBLIC Section. 
Data: df_public(25) Value 'This is from parent class’. 
Methods: DataflairParentM. 
ENDCLASS. 

CLASS DataflairChild Definition Inheriting From DataflairParent. 
PUBLIC Section. 
Methods: DataflairChildM. 
ENDCLASS. 

CLASS DataflairParent Implementation. 
Method DataflairParentM. 
Write /: df_public. 
EndMethod. ENDCLASS. 

CLASS DataflairChild Implementation. 
Method DataflairChildM. 
Skip. 
Write /: 'This is from child class', df_public.
EndMethod. 
ENDCLASS. 

Start-of-selection. 
Data: DataflairParent Type Ref To DataflairParent, 
DataflairChild Type Ref To DataflairChild. 
Create Object: DataflairParent, DataflairChild. 
Call Method: DataflairParent→DataflairParentM, 
child→DataflairChildM.

OUTPUT:

This is from parent class
This is from child class
This is from parent class

ABAP Access Control and Inheritance

  • We know that a class can be defined as public, private, protected
  • So when a subclass inherits a superclass, there are certain rules that govern how the class can access objects and data of superclass
  • The following table shows whether a derived class has access to base class, based on whether the base class is defined as public/protected/private:
ACCESSSAME CLASSDERIVED CLASSOUTSIDE (NON-DERIVED CLASS)
PUBLICYesYesAvailable
PRIVATEYesYesNo
PROTECTEDYesNoNo

The above table is explained as follows – 

1. Public Inheritance:

    • Public data and members of superclass become public data and members of subclass
    • Protected members of superclass become protected members of subclass
    • Private members of superclass cannot be accessed by subclass

2. Private Inheritance:

    • Public members of superclass become private members of subclass
    • Protected members of superclass become private members of subclass
    • Private members of superclass cannot be accessed by subclass

3. Protected Inheritance:

    • Public members of superclass become protected members of subclass
    • Protected members of superclass become protected members of subclass
    • Private members of superclass cannot be accessed by subclass

Redefining Methods in Sub Class in SAP ABAP

  • We can redefine methods of superclass in subclass
  • We usually do this so that we can have subclass-specific methods
  • However, we must keep the section of redefinition of the method the same as the parent method
  • We only need to use the name of the inherited method and can access its components using ‘super’ reference

Encapsulation in ABAP

  • Encapsulation in object orientation means wrapping data and functions together
  • This hides the data and function from the outside world, thus promoting data hiding
  • Data hiding means obstructing the view of private data and functions from unwanted third parties, and data abstraction means showing users only what they need to see
  • In ABAP, we can do encapsulation via access methods – public, private, protected.
  • We can also perform encapsulation via interfaces (which are similar to classes, the only difference is that we do not implement anything in an interface, it has to be done via class inheriting the interface, as shown in example below)

EXAMPLE OF ABAP ENCAPSULATION VIA INTERFACE

Report ZDATAFLAIR_ENCAPSULATION. 
Interface df_interface.
   Data text1 Type char35.
   Methods Dataflairmethod1.
EndInterface.

CLASS DataflairClass1 Definition.
   PUBLIC Section.
      Interfaces df_interface.
ENDCLASS. 

CLASS DataflairClass2 Definition.
   PUBLIC Section.
      Interfaces df_interface. 
ENDCLASS.

CLASS DataflairClass1 Implementation.
   Method df_interface~Dataflairmethod1.
      df_interface~text1 = 'DataflairClass 1 Interface method'.
      Write / df_interface~text1.
   EndMethod. 
ENDCLASS.
 
CLASS DataflairClass2 Implementation.
   Method df_interface~Dataflairmethod1.
      df_interface~text1 = 'DataflairClass 2 Interface method'.
      Write / df_interface~text1.
   EndMethod. 
ENDCLASS.
 
Start-Of-Selection.
   Data: DataflairObject1 Type Ref To DataflairClass1,
      DataflairObject2 Type Ref To DataflairClass2.
        
   Create Object: DataflairObject1, DataflairObject2.
   CALL Method: DataflairObject1→df_interface~Dataflairmethod1,
                DataflairObject2→df_interface~Dataflairmethod1. 

OUTPUT:

DataflairClass 1 Interface method
DataflairClass 2 Interface method

Polymorphism in SAP ABAP

  • It means using one thing for different operations
  • It occurs usually in inheritance – for e.g. redefining methods which we saw under the concept of inheritance
  • Polymorphism means redefining methods to either overload them or override them
  • Overloading methods means when we use the same method name but use different parameters
  • Overriding methods means when we use the same method name and parameters, but the two methods are related via inheritance relationship

ABAP Polymorphism Example(Redefining methods)

Report ZDATAFLAIR_POLYMORPHISM. 
CLASS df_class_02 Definition Abstract. 
PUBLIC Section. 
Methods: prgm_type Abstract, 
dfapproach1 Abstract. 
ENDCLASS. 

CLASS df_class_02 Definition 
Inheriting From df_class_01. 
PUBLIC Section. 
Methods: prgm_type Redefinition, 
approach1 Redefinition. 
ENDCLASS. 

CLASS class_procedural Implementation. 
Method prgm_type. 
Write: 'An apple'. 

EndMethod. Method approach1. 
Write: 'a fruit'. 

EndMethod. ENDCLASS. 
CLASS class_OO Definition 
Inheriting From class_prgm. 
PUBLIC Section. 
Methods: prgm_type Redefinition, 
approach1 Redefinition. 
ENDCLASS. 

CLASS class_OO Implementation. 
Method prgm_type. 
Write: 'An onion'. 
EndMethod. 

Method dfapproach1 . 
Write: 'a vegetable'.
EndMethod. 
ENDCLASS. 

CLASS class_type_approach Definition. 
PUBLIC Section. 
CLASS-METHODS: 
start Importing df_class_01_prgm 
Type Ref To class_prgm. 
ENDCLASS. 

CLASS class_type_approach IMPLEMENTATION. 
Method start. 
CALL Method df_class_01→prgm_type. 
Write: is. 

CALL Method df_class_01→approach1. 
EndMethod. 
ENDCLASS. 

Start-Of-Selection. 
Data: df_class_01 Type Ref To class_procedural, 
df_class_02 Type Ref To class_OO. 

Create Object df_class_01. 
Create Object df_class_02. 
CALL Method class_type_approach⇒start 
Exporting 

class1_prgm = df_class_01. 
New-Line. 
CALL Method class_type_approach⇒start 
Exporting 
class1_prgm = df_class_02.  

OUTPUT:

An apple is a fruit
An onion is a vegetable

Summary

Thus, in this tutorial we learnt more about the concepts of object orientation – inheritance, polymorphism, encapsulation – which form the core of reusability, data hiding and data abstraction properties of object-oriented programming, especially in ABAP.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

2 Responses

  1. pragyan sahoo says:

    i think here the table maintained here for PUBLIC ,PRIVATE AND PROTETED member accessibility needs a bot of modification.

    The Private members of the class is not visible to Derived class where as the protected members is visible

  2. Deepika says:

    Private and protected visibility needs to be corrected
    Private has visibility of same class, whereas protected is visible to same class and derived class

Leave a Reply

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