Object Oriented ABAP - Class can be instantiated within implementation of another class
This program will demonstrate that an object can be created from a class( (which was created with no CREATE PRIVATE|PROTECTED option at the time of its definition) in the implementation section of another class.
This program contains two classes – CLASS1 and CLASS2 .
Class CLASS1 contains method : METHOD1 which displays value of some integer variable.
Class CLASS2 contains method : METHOD2 . In the method implementation , an object is created from class : CLASS1 and then that object is used to call method METHOD1.
CLASS class1 DEFINITION.
METHODS : method1 .
CLASS class2 DEFINITION.
METHODS : method2 .
CLASS class1 IMPLEMENTATION.
DATA : i_num TYPE i VALUE 2.
CLASS class2 IMPLEMENTATION.
METHOD : method2.
DATA : obj1 TYPE REF TO class1.
CREATE OBJECT obj1.
CALL METHOD obj1->method1.
DATA : my_obj TYPE REF TO class2.
CREATE OBJECT : my_obj.
CALL METHOD my_obj->method2.