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.
CONSTRUCTOR( ) is called when we instantiate the object, whereas call of CLASS_CONSTRUCTOR( ) method is different based how the class is accessed.
Class Hierarchy UML
Let’s see it in details using this Classes. Each of this class has a CONSTRUCTOR( ), a CLASS-CONSTRUCTOR( ) and a Constant except class LCL_C.
Code Snippet
Code Snippet for class hierarchy setup:
*&---------------------------------------------------------------------*
*& 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
Code Snippet – Constant access Scenario
LOAD-OF-PROGRAM. |
Check this different scenarios:
This code accesses the constant C_D of the class LCL_D. It has executed all the CLASS_CONSTRUCTOR( ) of the higher classes in class hierarchy of class D i.e. Class LCL_A, LCL_B, LCL_C and LCL_D in the sequence. It didn’t execute the CLASS_CONSTRUCTOR( ) of the Class LCL_Z because we are not referring to any component of that class.
Code Snippet – Instantiation scenario
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( ).
Code Snippet – Complex
Which class-constructor calls first when both classes are not related?
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.
You can find more information of constructors on SAP Keyword documentation – Constructors
Check out all ABAP Objects Concepts
You would also like to explore more about other object oriented concepts in ABAP Objects:
- ABAP 740 – Is CONSTANT not a Static Attribute anymore?
- ABAP Abstract methods or Normal methods?
- ABAP Objects Events β SENDER, know who raised the event
- ABAP Objects Events – Raising and Handling
- ABAP Static vs Instance method – Which to use when?
- ABAP Protected Section – Is it really Protected?
- ABAP Objects – Achieve Multiple Inheritance using Interfaces
- Become a JAVAPER – “overriding” an attribute
- When to use Local Class and when not to!
- Abstract Class vs Interface – Which to use when?
- Override (Redefine) Static Method?
- Become a JAVAPER – is overloading possible in ABAP?
- Class based Exceptions IV – CLEANUP
- Class based Exceptions III – Runtime flow
- Class-based Exceptions II – Design Time consideration
- Class-based Exceptions I – Basics
- ABAP Objects Concepts – Friends
- READ-ONLY attribute vs GETTER methods
- CLASS_CONSTRUCTOR and CONSTRUCTOR: Who comes before whom?
- Persistent Object Service – Example
- Persistent Object Services – Basics
- ABAP Objects: Widening Cast
- ABAP Objects: Narrowing Cast
- ABAP Objects: Overriding (Redefinition)
Nicely explained!!!!
It's the best way of explaining static/instance constructor. Helps a lot in getting clear idea of this concept.
Wow… You are a true Contributor. Thanks very much.
Great !! .. Well explained with examples.. Thank you so much..
Very Good Explanation. Thanks a lot..:)
Thanks a lot…well explained
U r amazing! true didcation towards knowledge transfer! π
can you please tell me the purpose of a constructor?
Well explained sir!!!