SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

Object Oriented ABAP - Class can be instantiated within implementation of another class

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.

 


REPORT zstp_oo_abap.

CLASS class1 DEFINITION.
  PUBLIC SECTION.
    METHODS : method1 .
ENDCLASS.

CLASS class2 DEFINITION.
  PUBLIC SECTION.
    METHODS : method2 .
ENDCLASS.

CLASS class1 IMPLEMENTATION.
  METHOD :method1.
    DATA : i_num TYPE i VALUE 2.
    WRITE:/5 i_num.
  ENDMETHOD.
ENDCLASS.

CLASS class2 IMPLEMENTATION.
  METHOD : method2.
    DATA : obj1 TYPE REF TO class1.
    CREATE OBJECT obj1.
    CALL METHOD obj1->method1.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA : my_obj TYPE REF TO class2.
  CREATE OBJECT : my_obj.
  CALL METHOD my_obj->method2.

 

Output -

image

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in