Basic concepts of SAP ABAP objects and classes in detail. Here, we will learn about that Companying features of object orientation in 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.
Data: df_public(25) Value ‘This is from parent class’.
Methods: DataflairParentM.
CLASS DataflairChild Definition Inheriting From DataflairParent.
Methods: DataflairChildM.
CLASS DataflairParent Implementation.
CLASS DataflairChild Implementation.
Write /: ‘This is from child class‘, df_public.
Data: DataflairParent Type Ref To DataflairParent,
DataflairChild Type Ref To DataflairChild.
Create Object: DataflairParent, DataflairChild.
Call Method: DataflairParent→DataflairParentM,
OUTPUT:
This is from parent class
This is from parent class
ACCESS | SAME CLASS | DERIVED CLASS | OUTSIDE (NON-DERIVED CLASS) |
PUBLIC | Yes | Yes | Available |
PRIVATE | Yes | Yes | No |
PROTECTED | Yes | No | No |
1. Public Inheritance:
2. Private Inheritance:
3. Protected Inheritance:
Public members of superclass become protected members of subclass
EXAMPLE OF ABAP ENCAPSULATION VIA INTERFACE
Report ZDATAFLAIR_ENCAPSULATION.
Methods Dataflairmethod1.
CLASS DataflairClass1 Definition.
CLASS DataflairClass2 Definition.
CLASS DataflairClass1 Implementation.
Method df_interface~Dataflairmethod1.
df_interface~text1 = ‘DataflairClass 1 Interface method’.
Write / df_interface~text1.
CLASS DataflairClass2 Implementation.
Method df_interface~Dataflairmethod1.
df_interface~text1 = ‘DataflairClass 2 Interface method’.
Write / df_interface~text1.
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
ABAP Polymorphism Example(Redefining methods)
Report ZDATAFLAIR_POLYMORPHISM.
CLASS df_class_02 Definition Abstract.
Methods: prgm_type Abstract,
CLASS df_class_02 Definition
Inheriting From df_class_01.
Methods: prgm_type Redefinition,
CLASS class_procedural Implementation.
EndMethod. Method approach1.
Write: ‘a fruit’.
CLASS class_OO Definition
Inheriting From class_prgm.
Methods: prgm_type Redefinition,
CLASS class_OO Implementation.
CLASS class_type_approach Definition.
start Importing df_class_01_prgm
CLASS class_type_approach IMPLEMENTATION.
CALL Method df_class_01→prgm_type.
CALL Method df_class_01→approach1.
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
class1_prgm = df_class_01.
CALL Method class_type_approach⇒start
class1_prgm = df_class_02.
OUTPUT:
I hope this blog had explained basics 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.