Constructor and inheritance in ABAP Objects with example
Page 1 of 1
Constructor and inheritance in ABAP Objects with example
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 .
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
Similar topics
» Constructor methods ABAP Objects
» Static and instance constructor in ABAP Objects
» Parameterized constructor in ABAP
» Creating multiple Objects in ABAP Objects
» Difference between Procedural ABAP and ABAP Objects
» Static and instance constructor in ABAP Objects
» Parameterized constructor in ABAP
» Creating multiple Objects in ABAP Objects
» Difference between Procedural ABAP and ABAP Objects
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Mon Aug 20, 2012 4:37 pm by Admin
» Difference between Type and Like
Thu Jun 07, 2012 2:40 pm by Admin
» How to handle table control fields in Module Pool program?
Wed May 09, 2012 2:41 am by govind
» Three tables inner join in SAP
Thu May 03, 2012 4:47 pm by Uma_ABAP
» Creating table type in ABAP
Sat Apr 28, 2012 9:03 pm by Uma_ABAP
» Creating user defined types and structures in ABAP
Sat Apr 28, 2012 8:55 pm by Uma_ABAP
» Creating Dynamic Internal Table and Dynamic Field catalog in SAP-ABAP
Fri Apr 27, 2012 11:00 am by Admin
» Copy Custom PF-Status from Standard program to Custom Program in ALV
Thu Apr 26, 2012 9:52 pm by Uma_ABAP
» Calculate/find number of working days between two dates in SAP
Tue Apr 24, 2012 2:06 pm by Uma_ABAP