Site icon DataFlair

SAP ABAP OOPS – Inheritance, Encapsulation, Polymorphism

SAP ABAP OOPS

FREE Online Courses: Transform Your Career – Enroll for Free!

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

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

ACCESS SAME CLASS DERIVED CLASS OUTSIDE (NON-DERIVED CLASS)
PUBLIC Yes Yes Available
PRIVATE Yes Yes No
PROTECTED Yes No No

The above table is explained as follows – 

1. Public Inheritance:

2. Private Inheritance:

3. Protected Inheritance:

Redefining Methods in Sub Class in SAP ABAP

Encapsulation in ABAP

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

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

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.

Exit mobile version