Class - Constructor and CONSTRUCTOR: Who Comes Before Whom?
Class - Constructor and CONSTRUCTOR: Who Comes Before Whom?
Today we will see how the constructor and class-constructor triggers at runtime.
What is Constructor?
Lets see what is CONSTRUCTOR and CLASS_CONSTRUCTOR
CONSTRUCTOR
Whenever we instantiate the object using the statement CREATE OBJECT statement,
CONSTRUCTOR would be called. System creates an object before it calls the
CONSTRUCTOR method.
CLASS_CONSTRUCTOR
This type of constructor is the static constructor. This would be automatically accessed
when system accesses the class for the first time.
*&--------------------------------------------------------------------
-*
*& Developer : Naimesh Patel
*& Purpose : CLASS_CONSTRUCTOR and CONSTRUCTOR explaination
*&--------------------------------------------------------------------
-*
*
REPORT ZTEST_NP.
*
* ==== LCL_A ===== *
*
CLASS lcl_a DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: class_constructor.
CONSTANTS: c_a TYPE char1 VALUE 'A'.
METHODS: constructor.
ENDCLASS. "lcl_a DEFINITION
*
CLASS lcl_a IMPLEMENTATION.
METHOD class_constructor.
WRITE: / ' Class Constructor A'.
ENDMETHOD. "class_constructor
METHOD constructor.
WRITE: / ' Constructor A'.
ENDMETHOD. "constructor
ENDCLASS. "lcl_a IMPLEMENTATION
*
* ==== LCL_B ===== *
*
CLASS lcl_b DEFINITION INHERITING FROM lcl_a.
PUBLIC SECTION.
CONSTANTS: c_b TYPE char1 VALUE 'B'.
CLASS-METHODS: class_constructor.
METHODS constructor.
ENDCLASS. "lcl_b DEFINITION
*
CLASS lcl_b IMPLEMENTATION.
METHOD class_constructor.
WRITE : / ' Class Constructor B'.
ENDMETHOD. "class_constructor
METHOD constructor.
super->constructor( ).
WRITE : / ' Constructor B'.
ENDMETHOD. "constructor
ENDCLASS. "lcl_b IMPLEMENTATION
*
* ==== LCL_C ===== *
*
CLASS lcl_c DEFINITION INHERITING FROM lcl_b.
PUBLIC SECTION.
CLASS-METHODS: class_constructor.
METHODS constructor.
ENDCLASS. "lcl_b DEFINITION
*
CLASS lcl_c IMPLEMENTATION.
METHOD class_constructor.
WRITE : / ' Class Constructor C'.
ENDMETHOD. "class_constructor
METHOD constructor.
super->constructor( ).
WRITE : / ' Constructor C'.
ENDMETHOD. "constructor
ENDCLASS. "lcl_b IMPLEMENTATION
*
* ==== LCL_D ===== *
*
CLASS lcl_d DEFINITION INHERITING FROM lcl_c.
PUBLIC SECTION.
CONSTANTS: c_d TYPE char1 VALUE 'D'.
CLASS-METHODS: class_constructor.
METHODS constructor.
ENDCLASS. "lcl_b DEFINITION
*
CLASS lcl_d IMPLEMENTATION.
METHOD class_constructor.
WRITE : / ' Class Constructor D'.
ENDMETHOD. "class_constructor
METHOD constructor.
super->constructor( ).
WRITE : / ' Constructor D'.
ENDMETHOD. "constructor
ENDCLASS. "lcl_b IMPLEMENTATION
*
* ==== LCL_Z ===== *
*
CLASS lcl_z DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: class_constructor.
CONSTANTS: c_z TYPE char1 VALUE 'Z'.
METHODS: constructor.
ENDCLASS. "lcl_z DEFINITION
*
CLASS lcl_z IMPLEMENTATION.
METHOD class_constructor.
WRITE: / ' Class Constructor Z'.
ENDMETHOD. "class_constructor
METHOD constructor.
WRITE: / ' Constructor Z'.
ENDMETHOD. "constructor
ENDCLASS. "lcl_z IMPLEMENTATION
LOAD-OF-PROGRAM.
WRITE: / '... LOAD-OF-PROGRAM ...'.
*
START-OF-SELECTION.
SKIP 2.
WRITE: / '... START-OF-SELECTION ...'.
WRITE: / 'lcl_d=>c_d ..... ', lcl_d=>c_d.
LOAD-OF-PROGRAM.
WRITE: / '... LOAD-OF-PROGRAM ...'.
*
START-OF-SELECTION.
SKIP 2.
WRITE: / '... START-OF-SELECTION ...'.
WRITE: / 'Instantiating LO_D'.
DATA: lo_d TYPE REF TO lcl_d.
CREATE OBJECT lo_d.
Second scenario, in which we instantiate the object reference to LCL_D. You can see that
the CLASS_CONSTRUCTOR and CONSTRUCTOR were not called before START-OF-
SELECTION. They were actually called when the CREATE OBJECT statement reached.
So, instantiating the object has first called the method CLASS_CONSTRUCTOR( ) of the
higher classes in the class hierarchy and then all CONSTRUCTOR( ).
LOAD-OF-PROGRAM.
WRITE: / '... LOAD-OF-PROGRAM ...'.
*
START-OF-SELECTION.
SKIP 2.
WRITE: / '... START-OF-SELECTION ...'.
WRITE: / 'Instantiating LO_D'.
DATA: lo_d TYPE REF TO lcl_d.
CREATE OBJECT lo_d.
*
SKIP 1.
WRITE: / 'lcl_z=>c_z ..... ', lcl_z=>c_z.
WRITE: / 'lcl_b=>c_b ..... ', lcl_b=>c_b.
Wow! Too many different execution paths! System first encountered LCL_Z=>C_Z. So, it
has called the CLASS_CONSTRUCTOR of the LCL_Z. After that it encountered
LCL_B=>C_B. Thus, it calls the CLASS_CONSTRUCTOR of the class hierarchy of the
class LCL_B i.e. CLASS_CONSTRUCTOR of classes LCL_A and LCL_B.
Now, when we instantiated the object LO_D, system calls the methods
CLASS_CONSTRUCTOR( ) of the remaining classes of the class hierarchies i.e. method
CLASS_CONSTRUCTOR( ) of classes LCL_C and LCL_D. It doesn’t call
CLASS_CONSTRUCTOR( ) of the classes LCL_A and LCL_B, because they were already
called when it has encountered the access to constant LCL_B=>C_B.
A user who is not interested in the finer points of cars, trucks, and busses (but only, for example, in the
fuel consumption and tank gauge) does not need to know about them. This user only wants and needs to
work with (references to) the lcl_vehicle(super class) class. However, in order to allow the user to work
with cars, busses, or trucks, you generally need a narrowing cast.
1. In narrowing casting the object which is created with reference to the sub class is assigned to the
reference of type super class.
2. Using the super class reference it is possible to access the methods from the object which are
only defined at the super class.
3. This access is also called as generic access as super class is normally called as general class.
Example:
Here method4 is the specific for the sub class and remaining methods are inherited from the super
class.
Narrowing cast:
REF_VEHICLE = REF_TRUCK.
1. By the super class reference (REF_VEHICLE) it is possible to access all the methods which are
defined at the super class but the implementations are taken from the sub class.
2. If any method is redefined at the sub class then that method’s implementation which exist at the sub
class is taken in to consideration.
Like:
Here we can access the implementation which exists at the sub class but not from the super class.
3. It is not possible to access the methods which only defined in the sub class using the super class
reference.
Definition: - The assignment of a superclass instance to a reference variable of the type "points to sub
class" is described as a Wide casting, because here we are switching from a more generalized view to a
one with more detail (Specialized View).
Note:- In order to implement wide casting first we need to implement the narrow casting.
2. Steps:-
Here I am taking the example of calculation of area of rectangle and area of cube.
In the above picture the class class_super is the super class and has one method area which is to
find the area of rectangle. The method has two parameters w_length, w_breadth for length and
breadth.
b) Definition and implementation of sub class:-
In the above picture the class class_sub is the sub class which is inheriting from the super
classclass_super. This sub class has one method area which is same as that of super class and
redefined for calculating the area of cube. This method has one more parameter W_height along with
w_length and w_breadth. This method finds the area of cube.
In above screen two reference variables have been declared for super class and one reference
variable is declared for sub class.
d) Creation of objects:-
Now create the objects for the super and base class.
We are assigning the value ‘10’ to ‘w_height’ instance variable of subclass object.
In above picture we are assigning the sub class instance reference variable to super class instance
reference variable. This is known as narrow casting.
Now the instance of the super class(object_superclass) now points or refers to the object of the subclass,
thus the modified method 'area' of the subclass is accessible. Now we can call the method Area of
subclass class_sub to calculate the area of cube.
a) Implementation of Wide casting:- From below screen shot we implement the wide casting.
The object 'object_superclass' now refers to the object of the subclass'object_subclass'. Thus
the 'area' method of the superclass cannot be called by this object. In order to enable it to call
the 'area' method of the superclass, wide casting has to be performed.
So right hand side of wide casting always has object of super class. Left side of wide casting always is
object reference declared as ‘type ref to’ to super class. Now method area of super class is called again
to find the area of rectangle.
Note:- Wide Casting cannot be performed on sub class objects. It can only be performed on super class
objects that have narrow casting.
1. Summary:- The assignment of a superclass instance to a reference variable of the type "points to
sub class" is described as a Wide casting, because here we are switching from a more generalized
view to a one with more detail(Specialized View).
2. Source Code:-
DATA:
object_superclass TYPE REF TO class_super,
object_subclass TYPE REF TO class_sub,
object2_superclass TYPE REF TO class_super.
START-OF-SELECTION.
CREATE OBJECT object_superclass.
CREATE OBJECT object2_superclass.
CREATE OBJECT object_subclass.
"Narrow casting
object_subclass->w_height = 10.
object_superclass = object_subclass.
"Wide casting
object_superclass ?= object2_superclass.
Output:-
Achieving Multiple Inheritance
By Raja Narayanan
ABAP objects don’t support multiple inheritances using more than one class.
Here I have considered a sample scenario which demonstrates how we can achieve the multiple
inheritances in ABAP OO.
Create a class called ZCL_CAR_DETAILS and add these interface’s as shown below.
Implement the method ZIF_VECHICLE_MODEL~GET_MODEL and write your logic.
The similar way we will create another class called ZCL_BIKE_DETAIL which again implement the same
interface’s.
Now we will create a new class called ZCL_VECHICLE which will use both the above classes (indirectly
achieving Multiple Inheritance here).
This is a simple tutorial showing how we can create table of references and taking the advantage of
polymorphism concept we can invoke the different implementation of the same method of the different
subclasses using a single object.
Example:
Super class definition of Class animal. Animal being a generic class will not have any behaviour and
hence is declared as abstract.
Subclass Dog which inherits from class animal and creates its own implementation for the method
make_noise.
Subclass Dog which inherits from class animal and creates its own implementation for the method
make_noise.
Now we create a table of references of type animal and we will use this reference type to invoke the
methods of both dog and cat class.
Now taking the advantage of polymorphism the super class being unaware of the implementation of the
method make_noise can we can use the super class reference to invoke the methods of sub class.
Output:
Code :
*&---------------------------------------------------------------------*
*& Report YCL_POLYMORPHISM
*&
*&---------------------------------------------------------------------*
REPORT ycl_polymorphism.
*----------------------------------------------------------------------*
* CLASS animal DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS animal DEFINITION ABSTRACT.
PUBLIC SECTION.
DATA : noise TYPE char10.
METHODS make_noise.
*----------------------------------------------------------------------*
* CLASS animal IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS animal IMPLEMENTATION.
METHOD make_noise.
ENDMETHOD. "make_noise
*----------------------------------------------------------------------*
* CLASS dog DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS dog DEFINITION INHERITING FROM animal.
PUBLIC SECTION.
DATA play TYPE char10.
METHODS make_noise REDEFINITION.
*----------------------------------------------------------------------*
* CLASS dog IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS dog IMPLEMENTATION.
METHOD make_noise.
noise = 'Bark'.
play = 'Wag tail'.
WRITE / : noise,
play.
SKIP.
ENDMETHOD. "make_noise
*----------------------------------------------------------------------*
* CLASS cat DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cat DEFINITION INHERITING FROM animal.
PUBLIC SECTION.
METHOD make_noise.
noise = 'Meow'.
WRITE / noise.
SKIP.
ENDMETHOD. "make_noise
START-OF-SELECTION.
wa_animal = ref_dog.
APPEND wa_animal TO ref_animal.
wa_animal = ref_cat.
APPEND wa_animal TO ref_animal.
wa_animal = ref_dog.
APPEND wa_animal TO ref_animal.
LOOP AT ref_animal
ASSIGNING <fs_animal>.
CALL METHOD <fs_animal>->make_noise.
ENDLOOP.