‎2008 Jul 21 10:52 AM
‎2008 Jul 21 12:32 PM
Hi 'abap learner',
I guess u meant 'Can constructors take part in inheritance?'
YES! is the answer. You can implement a constructor in a subclass. Anything more about this depends on whether you're talking about the instance constructor or the class constructor.
If its Instance constructor:
When we implement an instance constructor for a subclass, before doing anything, we have to explicitly call the super class instance constructor, regardless of whether an instance constructor has been declared for the super class or not. For this, we use the pseudo reference 'super' as below.
super->constructor( ).
Example:
*----------------------------------------------------------------------*
* CLASS c1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class C1 definition.
public section.
methods: M1
, CONSTRUCTOR.
protected section.
data ID type I.
endclass. "c1 DEFINITION
*----------------------------------------------------------------------*
* CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class C1 implementation.
method M1.
write: / 'This is from method m1 - ID: ', ID.
endmethod. "m1
method CONSTRUCTOR.
ID = 1.
write: /'assigned super'.
endmethod. "constructor
endclass. "c1 IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS C2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class C2 definition inheriting from C1.
public section.
methods: M2
, CONSTRUCTOR.
private section.
data ID2 type I.
endclass. "c1 DEFINITION
*----------------------------------------------------------------------*
* CLASS C2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class C2 implementation.
method M2.
uline.
write: /10 'This is from method m2 - ID: ', ID.
write: /10 'This is from method m2 - ID2: ', ID2.
uline.
endmethod. "m1
method CONSTRUCTOR.
*The following statement should be the first statement in a *subclass constructor.
SUPER->CONSTRUCTOR( ).
write: /10 'assigned sub'.
ID = 9.
ID2 = 1.
endmethod. "constructor
endclass. "c1 IMPLEMENTATION
data C1REF type ref to C1.
data C2REF type ref to C2.
start-of-selection.
create object C1REF.
C1REF->M1( ).
skip 3.
create object C2REF.
C2REF->M2( ).Class constructor/Static Constructor:
Unlike the instance constructor, while implementing a Class constructor in a subclass, we DO NOT have to explicitely call the super class constructor. During the start of execution of a class constructor (say of class c5), the class constructor of its immediate superclass (say c4) is accessed automatically. ABAP runtime doesn't execute any class constructor unless it reaches the top most class in the inheritance tree (say c1) . It then executes the class constructor of the top most class, followed by that of its subclass and so on until it reaches the class which initiated the whole process (c5).
Example:
*----------------------------------------------------------------------*
* CLASS c1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class C1 definition.
public section.
class-methods CLASS_CONSTRUCTOR.
endclass. "c1 DEFINITION
*----------------------------------------------------------------------*
* CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class C1 implementation.
method CLASS_CONSTRUCTOR.
write: /'Super class created.'.
endmethod. "constructor
endclass. "c1 IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS C2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class C2 definition inheriting from C1.
public section.
class-methods CLASS_CONSTRUCTOR.
endclass. "c1 DEFINITION
*----------------------------------------------------------------------*
* CLASS C2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class C2 implementation.
method CLASS_CONSTRUCTOR.
write: /10 'Subclass created.'.
endmethod. "constructor
endclass. "c1 IMPLEMENTATION
data C2REF type ref to C2.
start-of-selection.
create object C2REF.
‎2008 Jul 21 11:24 AM
Hi
*No, constructors are not inherited*. Nor are they considered virtual
methods. If you do not provide a constructor in class Foo, the
compiler will synthesize which will fail in compilation if the superclass doesn't have a
parameterless constructor.
A superclass' constructors are only available to a class through the
super keyword.
Also,
Constructors and methods use the keyword this quite differently. A method uses this to refer to the instance of the class that is executing the method. Static methods do not use this; they do not belong to a class instance, so this would have nothing to reference. Static methods belong to the class as a whole, rather than to an instance. Constructors use this to refer to another constructor in the same class with a different parameter list.
Cheers
Mohinder Singh
‎2008 Jul 21 12:04 PM
Constructors cannot be inherited. We call the constructor of superclass from subclass by using the keyword SUPER.
The superclass's constructor can be used without any need to change it, or the subclass can be expanded and other parametsrs will be required in constructor's signature.
In ABAP Objects, the constructor can only be overwritten as a part of inheritance. It can be overwritten in the sense that both the signature and implementation can be adjusted in the subclass.
The constructor of the direct superclass must be called within the constructor of sublasss. This is because of specialization relationship. If a constructor was defined in superclass, it contains implementations that will always be executed when an object is created in this superclass or its subclasses. However, this can only be automaticaaly ensured by runtime system if the subclass's constructor was not changed. In most cases, supplying consistent private attributes from superclass is another prerequisite to call superclass constructor.
NOTE: IF A SUBCLASS HAS NOT CHANGED ITS CONSTRUCTOR, THE CONSTRUCTOR IS LEFT UNCHANGED AND IS ADOPTED FROM SUPERCLASS. THE IMPLEMENTATION IS THEN EXECUTED FROM THE SUPERCLASS.
Regards
Abhijeet
‎2008 Jul 21 12:06 PM
no.
constructors can not be inherited,
please search in sdn by giving key word constructors..
pradeep
‎2008 Jul 21 12:09 PM
Hi,
Just have a look at this code(only the constructor part of superclass and subclass).
REPORT z_CLASS_CASTING.
TYPE-POOLS icon.
INCLUDE z_class.
DATA: r_plane TYPE REF TO lcl_airplane,
r_cargo TYPE REF TO lcl_cargo_plane,
r_passenger TYPE REF TO lcl_passenger_plane,
plane_list TYPE TABLE OF REF TO lcl_airplane.
START-OF-SELECTION.
lcl_airplane=>display_n_o_airplanes( ).
CREATE OBJECT r_passenger EXPORTING
im_name = 'LH BERLIN'
im_planetype = '747-400'
im_seats = 345.
APPEND r_passenger TO plane_list.
CREATE OBJECT r_cargo EXPORTING
im_name = 'US Hercules'
im_planetype = '747-500'
im_cargo = 533.
APPEND r_cargo TO plane_list.
LOOP AT plane_list INTO r_plane.
r_plane->display_attributes( ).
ENDLOOP.
lcl_airplane=>display_n_o_airplanes( ).
INCLUDE PROGRAM
*&---------------------------------------------------------------------*
*& Include z_CLASS *
*&---------------------------------------------------------------------*
CLASS lcl_airplane DEFINITION.
PUBLIC SECTION.
CONSTANTS: pos_1 TYPE i VALUE 30.
METHODS:
constructor IMPORTING
im_name TYPE string
im_planetype TYPE saplane-planetype,
display_attributes.
CLASS-METHODS: display_n_o_airplanes.
PRIVATE SECTION.
METHODS: get_technical_attributes
IMPORTING im_type TYPE saplane-planetype
EXPORTING ex_weight TYPE s_plan_wei
ex_tankcap TYPE s_capacity.
DATA: name TYPE string,
planetype TYPE saplane-planetype.
CLASS-DATA: n_o_airplanes TYPE i.
ENDCLASS. "lcl_airplane DEFINITION
*---------------------------------------------------------------------*
* CLASS lcl_airplane IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_airplane IMPLEMENTATION.
METHOD constructor.
name = im_name.
planetype = im_planetype.
n_o_airplanes = n_o_airplanes + 1.
ENDMETHOD. "constructor
METHOD display_attributes.
DATA: weight TYPE saplane-weight,
cap TYPE saplane-tankcap.
WRITE: / icon_ws_plane AS ICON,
/ 'Name of airplane', AT pos_1 name,
/ 'Airplane type:', AT pos_1 planetype.
get_technical_attributes( EXPORTING im_type = planetype
IMPORTING ex_weight = weight
ex_tankcap = cap ).
WRITE: / 'Weight:', weight,
'Tank capacity:', cap.
ENDMETHOD. "display_attributes
METHOD display_n_o_airplanes.
WRITE: /,/ 'Number of airplanes:',AT pos_1 n_o_airplanes
LEFT-JUSTIFIED, /.
ENDMETHOD. "display_n_o_airplanes
METHOD get_technical_attributes.
SELECT SINGLE weight
tankcap
FROM saplane
INTO (ex_weight, ex_tankcap)
WHERE planetype = im_type.
IF sy-subrc <> 0.
ex_weight = 100000.
ex_tankcap = 10000.
ENDIF.
ENDMETHOD. "get_technical_attributes
ENDCLASS. "lcl_airplane IMPLEMENTATION
*---------------------------------------------------------------------*
* CLASS lcl_cargo_plane DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_cargo_plane DEFINITION INHERITING FROM lcl_airplane.
PUBLIC SECTION.
METHODS: constructor IMPORTING im_name TYPE string
im_planetype TYPE saplane-planetype
im_cargo TYPE scplane-cargomax.
METHODS: display_attributes REDEFINITION.
METHODS: get_cargo RETURNING value(re_cargo) TYPE scplane-cargomax.
PRIVATE SECTION.
DATA: max_cargo TYPE scplane-cargomax.
ENDCLASS. "lcl_cargo_plane DEFINITION
*---------------------------------------------------------------------*
* CLASS lcl_cargo_plane IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_cargo_plane IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor( im_name = im_name
im_planetype = im_planetype ).
max_cargo = im_cargo.
ENDMETHOD. "constructor
METHOD display_attributes.
super->display_attributes( ).
WRITE: / 'Max Cargo = ', max_cargo.
ULINE.
ENDMETHOD. "display_attributes
METHOD get_cargo.
re_cargo = max_cargo.
ENDMETHOD. "get_cargo
ENDCLASS. "lcl_cargo_plane IMPLEMENTATION
*---------------------------------------------------------------------*
* CLASS lcl_passenger_plane DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_passenger_plane DEFINITION INHERITING FROM lcl_airplane.
PUBLIC SECTION.
METHODS: constructor IMPORTING im_name TYPE string
im_planetype TYPE saplane-planetype
im_seats TYPE sflight-seatsmax.
METHODS: display_attributes REDEFINITION.
PRIVATE SECTION.
DATA: max_seats TYPE sflight-seatsmax.
ENDCLASS. "lcl_passenger_plane DEFINITION
*---------------------------------------------------------------------*
* CLASS lcl_passenger_plane IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_passenger_plane IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor( EXPORTING im_name = im_name
im_planetype = im_planetype ).
max_seats = im_seats.
ENDMETHOD. "constructor
METHOD display_attributes.
super->display_attributes( ).
WRITE: / 'Max Seats = ', max_seats.
ULINE.
ENDMETHOD. "display_attributes
ENDCLASS. "lcl_passenger_plane IMPLEMENTATION
‎2008 Jul 21 12:32 PM
Hi 'abap learner',
I guess u meant 'Can constructors take part in inheritance?'
YES! is the answer. You can implement a constructor in a subclass. Anything more about this depends on whether you're talking about the instance constructor or the class constructor.
If its Instance constructor:
When we implement an instance constructor for a subclass, before doing anything, we have to explicitly call the super class instance constructor, regardless of whether an instance constructor has been declared for the super class or not. For this, we use the pseudo reference 'super' as below.
super->constructor( ).
Example:
*----------------------------------------------------------------------*
* CLASS c1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class C1 definition.
public section.
methods: M1
, CONSTRUCTOR.
protected section.
data ID type I.
endclass. "c1 DEFINITION
*----------------------------------------------------------------------*
* CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class C1 implementation.
method M1.
write: / 'This is from method m1 - ID: ', ID.
endmethod. "m1
method CONSTRUCTOR.
ID = 1.
write: /'assigned super'.
endmethod. "constructor
endclass. "c1 IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS C2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class C2 definition inheriting from C1.
public section.
methods: M2
, CONSTRUCTOR.
private section.
data ID2 type I.
endclass. "c1 DEFINITION
*----------------------------------------------------------------------*
* CLASS C2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class C2 implementation.
method M2.
uline.
write: /10 'This is from method m2 - ID: ', ID.
write: /10 'This is from method m2 - ID2: ', ID2.
uline.
endmethod. "m1
method CONSTRUCTOR.
*The following statement should be the first statement in a *subclass constructor.
SUPER->CONSTRUCTOR( ).
write: /10 'assigned sub'.
ID = 9.
ID2 = 1.
endmethod. "constructor
endclass. "c1 IMPLEMENTATION
data C1REF type ref to C1.
data C2REF type ref to C2.
start-of-selection.
create object C1REF.
C1REF->M1( ).
skip 3.
create object C2REF.
C2REF->M2( ).Class constructor/Static Constructor:
Unlike the instance constructor, while implementing a Class constructor in a subclass, we DO NOT have to explicitely call the super class constructor. During the start of execution of a class constructor (say of class c5), the class constructor of its immediate superclass (say c4) is accessed automatically. ABAP runtime doesn't execute any class constructor unless it reaches the top most class in the inheritance tree (say c1) . It then executes the class constructor of the top most class, followed by that of its subclass and so on until it reaches the class which initiated the whole process (c5).
Example:
*----------------------------------------------------------------------*
* CLASS c1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class C1 definition.
public section.
class-methods CLASS_CONSTRUCTOR.
endclass. "c1 DEFINITION
*----------------------------------------------------------------------*
* CLASS c1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class C1 implementation.
method CLASS_CONSTRUCTOR.
write: /'Super class created.'.
endmethod. "constructor
endclass. "c1 IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS C2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class C2 definition inheriting from C1.
public section.
class-methods CLASS_CONSTRUCTOR.
endclass. "c1 DEFINITION
*----------------------------------------------------------------------*
* CLASS C2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class C2 implementation.
method CLASS_CONSTRUCTOR.
write: /10 'Subclass created.'.
endmethod. "constructor
endclass. "c1 IMPLEMENTATION
data C2REF type ref to C2.
start-of-selection.
create object C2REF.
‎2008 Jul 21 12:51 PM
Hi,
Constructors cannot be inherited to the subclass. It can only be called within the subclass using super to initialize the members of super class.
Thanks,
Naveen Kumar.
‎2008 Jul 21 1:08 PM
Hi Naveen,
You seem to have forgotten one thing. I don't think - "It can only be called from the subclass".
In fact, it can be implemented in the subclass, or in correct terms, redefined in the subclass, thus allowing some polymorphism.
*-------CLASS C1 implementation--------------
method CONSTRUCTOR. "super-class constructor
ID = 1.
write: /'assigned super'.
endmethod. "constructor
.
.
.
*-------CLASS C2 implementation--------------
method CONSTRUCTOR. "implemented/redefined in subclass
SUPER->CONSTRUCTOR( ).
write: /10 'assigned sub'.
ID = 9.
ID2 = 1.
endmethod. "constructorAs seen above, when the class C2 is instantiated, the subclass constructor overrides the superclass constructor. Thus, value of ID will be 9 and not 1.
Correct me if i'm wrong.
‎2008 Jul 21 1:36 PM
Hi Arun,
Super class Constructor cannot be implemented/redefined in a subclass since you are calling super class constructor within sub class constructor.
Thanks,
Naveen Kumar.
‎2008 Jul 21 2:01 PM
I see a point in that, since we're explicitly calling the super class constructor no matter what.
In that case, I don't see polymorphism there.
But at the same time, in my example, when the subclass is instantiated, it assigns a different value (9) to the super class protected variable (ID). Doesn't it smell like redefinition there?
However, thanks for pointing out that vital point.
‎2008 Jul 22 8:15 AM
Hi Arun,
Using OO ABAP we cannot implement polymorphism(overloading/overriding) using constructors.
Thanks,
Naveen Kumar.
‎2008 Jul 23 6:16 AM
Hi Naveen,
Again I feel that your statement is debatable. I hope you've heard about the concept of runtime polymorphism - The feature by virtue of which out of many available implementations, the correct implementation is chosen by the type of instance (or to which class the instance belongs to).
Lets consider the same example:
*-------SUPERCLASS C1 implementation--------------
method CONSTRUCTOR. "super-class constructor
ID = 1.
write: /'assigned super'.
endmethod. "constructor
.
.
.
*-------SUBCLASS C2 implementation--------------
method CONSTRUCTOR. "implemented/redefined in subclass
SUPER->CONSTRUCTOR( ).
write: /10 'assigned sub'.
ID = 9.
ID2 = 1.
endmethod. "constructorHere the superclass constructor sets the value of ID to 1. (ID is a protected attribute of Class C1). The subclass constructor sets ID to 9.
If we instantiate class C1, the value of ID will be 1. If we instantiate C2, the value of ID will be 9. So the implementation (here, setting the value of ID) depends on the instance right?Hence, runtime polymorphism holds good! Don't you think so?
‎2008 Jul 23 3:24 PM
Hi Arun,
It is not actually implemented/redefined in subclass. Both Sub class and super class constructors are independent entities. Please recollect in C++ constructor name is same as class name.
Thanks,
Naveen Kumar.
‎2008 Jul 24 6:53 AM
‎2008 Jul 24 8:06 AM
HI ,
I came to a coclusion that Constructors can be inherited
check this simple example
CLASS cl_lc DEFINITION.
PUBLIC SECTION.
DATA: a TYPE i,
b TYPE i,
c TYPE i.
METHODS: display,
mm1,
CONSTRUCTOR.
CLASS-METHODS: mm2.
ENDCLASS. "CL_LC DEFINITION
*CLASS IMPLEMENTATION
CLASS cl_lc IMPLEMENTATION.
METHOD display.
WRITE:/ 'THIS IS SUPER CLASS' COLOR 7.
ENDMETHOD. "DISPLAY
METHOD mm1.
WRITE:/ 'THIS IS MM1 METHOD IN SUPER CLASS'.
ENDMETHOD. "MM1
METHOD mm2.
WRITE:/ 'THIS IS THE STATIC METHOD' COLOR 2.
WRITE:/ 'THIS IS MM2 METHOD IN SUPER CLASS' COLOR 2.
ENDMETHOD. "MM2
method CONSTRUCTOR.
write :/ 'I am in Constructor'.
endmethod.
ENDCLASS. "CL_LC IMPLEMENTATION
*SUB CLASS FUNCTIONALITY
*CREATE THE CLASS.
*INHERITING THE SUPER CLASS.
CLASS cl_sub DEFINITION INHERITING FROM cl_lc. "HOW WE CAN INHERIT
PUBLIC SECTION.
DATA: a1 TYPE i,
b1 TYPE i,
c1 TYPE i.
METHODS: display REDEFINITION, "REDEFINE THE SUPER CLASS METHOD
sub.
ENDCLASS. "CL_SUB DEFINITION
*CLASS IMPLEMENTATION.
CLASS cl_sub IMPLEMENTATION.
METHOD display.
WRITE:/ 'THIS IS THE SUB CLASS OVERWRITE METHOD' COLOR 3.
ENDMETHOD. "DISPLAY
METHOD sub.
WRITE:/ 'THIS IS THE SUB CLASS METHOD' COLOR 3.
ENDMETHOD. "SUB
ENDCLASS. "CL_SUB IMPLEMENTATION
*CREATE THE OBJECT FOR SUB CLASS.
DATA: obj TYPE REF TO cl_sub.
START-OF-SELECTION.
CREATE OBJECT obj.
CALL METHOD obj->display. "THIS IS SUB CLASS METHOD
CALL METHOD obj->sub.
WRITE:/'THIS IS THE SUPER CLASS METHODS CALLED BY THE SUB CLASS OBJECT'COLOR 5.
SKIP 1.
CALL METHOD obj->mm1. "THIS IS SUPER CLASS METHOD
CALL METHOD obj->mm2.
regards
‎2008 Jul 28 3:12 PM
Bingo! There u are abaplearner!!
Naveen, I understand what you are saying. Agree with you. But what I am trying to say is that, we can actually simulate a redefinition here. Hope you tried out the example in my previous post.