Constructor and inheritance in ABAP Objects with example

Go down

Constructor and inheritance in ABAP Objects with example

Post  Uma_ABAP on Sun Apr 01, 2012 1:16 pm

In the earlier section we have seen the concept of inheritance ,how can we access the
methods of the super class etc. From our earlier exposure to constructor we know that
every class has a static as well as instance constructor ,and they are implicitly defined by the system . Therefore in an inheritance tree the super class as well as the subclass has an unique constructor . In this section we plan to look into some of the issues which might come up, lets take them point wise.

suppose we instantiate a sub class ,what happens to the superclass whose constructor
has not been defined explicitly?


When ever we instantiate a sub class ,whose super class constructor is not explicitly
defined ,then the constructor of the super class is called automatically by the
system ,we need not take any extra care during subclass instantiation .

suppose we instantiate a sub class which has an explicitly defined constructor ,and the super class constructor is not defined explicitly then what happens?

In cases where the super class constructor is not defined explicitly ,the system
automatically calls the the super class constructor during instantiation of
subclass.The sequence in which the constructor are called is same as that of the
inheritance hierarchy i.e grandfather-> father-> child

suppose we instantiate a sub class which has an explicitly defined constructor and the super class constructor is also explicitly defined then what happens during the instantaneous of sub class?

In cases where the super class constructor is explicitly defined ,the system cannot
call the constructor of the super class during subclass instantiation ,the super class
constructor has to be explicitly coded in the sub class constructor I,e we should
write the code as

Call method super->constructor importing <parameters>

if suppose the super constructor is explicitly defined and has some input parameters then how do we pass the parameters to the superclass constructor from the sub class?

To pass parameters to the super constructor we must do it at the time of instantiation of the sub class ,constructor of the subclass has to be defined explicitly and parameters has to be passed to it ,in the definition part of the subclass constructor the super class constructor has to be called and then parameters has to be passed to it .

Code:

********************************************************************
* gf_num importing parameter for grandfather constructor
* f_num importing parameter for father constructor
* c_num importing parameter for child constructor.
* this example shows how parameters can be passed to super constructor from
*subclass constructor other methods may also exist.
**
*this is a full working program showing constructor and inheritance
*******************************************************************
CLASS grand_father DEFINITION.
PUBLIC SECTION .
METHODS : gf_property,
constructor importing gf_num type i.
ENDCLASS.

CLASS grand_father IMPLEMENTATION.
METHOD gf_property.
write:/ 'i am grand father'.
endmethod.
method constructor .
write:/ 'grand father constructor'.
write:/ gf_num.
endmethod.
ENDCLASS.

CLASS father DEFINITION INHERITING FROM grand_father.
PUBLIC SECTION.
METHODS:f_property,
constructor importing f_num type i gf_num type i.
ENDCLASS.

CLASS father IMPLEMENTATION.
METHOD f_property.
write:/ 'i am father'.
ENDMETHOD.
METHOD constructor.
call method super->constructor exporting gf_num = gf_num.
******/constructor of grandfather is called from father constructor
write:/ 'father constructor'.
write:/ f_num.
ENDMETHOD.
ENDCLASS.

CLASS child DEFINITION INHERITING FROM father.
PUBLIC SECTION.
METHODS:c_property,
constructor importing c_num type i
f_num type i
gf_num type i.
ENDCLASS.

CLASS child IMPLEMENTATION.
METHOD c_property.
write:/'i am child'.
ENDMETHOD.
METHOD constructor.
call method super->constructor exporting f_num = f_num gf_num = gf_num
*******/constructor of father is called from the child constructor
write:/ 'child constructor'.
write:c_num.
ENDMETHOD.
ENDCLASS.

******************* main program*****************8
start-of-selection.
data: my_obj TYPE REF TO CHILD.
CREATE OBJECT: my_obj exporting c_num = 3 f_num = 2 gf_num = 3.
call method my_obj->gf_property. “ calling the method of class grandfather
call method my_obj->f_property. “calling the method of class father
end-of-selection.

Uma_ABAP

Posts : 56
Join date : 2012-03-31

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum