Sie sind auf Seite 1von 23

CLASS_CONSTRUCTOR and

CONSTRUCTOR: Who comes before


whom?

 Constructor is automatically called when an object created.


 Constructor is the same name of the class.
 No return value.
 With in static method we can only access class attributes.
 Class-constructor does not have any parameters.
 Constructor has only import parameters.

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.
WRITE: / '... LOAD-OF-PROGRAM ...'.
*
START-OF-SELECTION.
SKIP 2.
WRITE: / '... START-OF-SELECTION ...'.
WRITE: / 'lcl_d=>c_d ..... ', lcl_d=>c_d.

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

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( ).

Code Snippet – Complex


Which class-constructor calls first when both classes are not related?

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.

WIDE Casting vs Narrow Casting.


Definition: The assignment of a subclass instance to a reference variable of the type "reference to super
class" is described as a narrowing cast, because you are switching from a more detailed view to a one
with less detail. It is also called as up-casting.

Use of narrowing casting:

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.

Principle of narrowing casting:

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:

Super class: vehicle (contains general methods)


Sub class: truck (contains more specific methods)

Here method4 is the specific for the sub class and remaining methods are inherited from the super
class.

Now create the object with reference to the subclass.

1. Declare a variable with reference to the subclass.


DATA: REF_TRUCK TYPE REF TO TRUCK.

2. Create object with this reference.

CREATE OBJECT REF_TRUCK.

Narrowing cast:

1. Declare a variable with reference to the super class.

DATA: REF_VEHICLE TYPE REF TO VEHICLE.

2. Assign the object reference (REF_TRUCK) to REF_VEHICLE.

REF_VEHICLE = REF_TRUCK.

Accessing methods using super class reference.

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.

E.g. assume that ‘method2’ is redefined at the sub class.

When this method is accessed using the super class reference

Like:

Call method REF_VEHICLE->method2.

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.

E.g. Method4 is not accessed using reference REF_VEHICLE.

Call method REF_VEHICLE-> Method4.

This is wrong convention.


Tutorial on Wide casting
By Vineesh Batchu

1. Introduction: - This tutorial is of OOP’s ABAP on concept of Wide casting.

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.

a) Definition and implementation of Super Class:-

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.

c) Declaration of reference variables:-

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.

Call the method Area of super class as below:

e) Implementation of Narrow casting:- As I discussed in introduction in order to implement wide


casting we need to first implement narrow casting.

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:-

REPORT yh_wide NO STANDARD PAGE HEADING.

CLASS class_super DEFINITION.


PUBLIC SECTION.
DATA:
w_area TYPE i.
METHODS:
area IMPORTING w_length TYPE i w_breadth TYPE i.
ENDCLASS. "class_super DEFINITION

CLASS class_super IMPLEMENTATION.


METHOD area.
w_area = w_length * w_breadth.

WRITE:/ 'Area of rectangle = '.


WRITE: w_area.
ENDMETHOD. "area
ENDCLASS. "class_super IMPLEMENTATION

CLASS class_sub DEFINITION INHERITING FROM class_super.


PUBLIC SECTION.
DATA:
w_height TYPE i.
METHODS:
area REDEFINITION.
ENDCLASS. "class_sub DEFINITION

CLASS class_sub IMPLEMENTATION.


METHOD area.
w_area = 6 * w_length * w_breadth * w_height.
WRITE:/ 'Area of the cube ='.
WRITE: w_area.
ENDMETHOD. "area
ENDCLASS. "class_sub IMPLEMENTATION

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.

CALL METHOD object_superclass->area


EXPORTING
w_length = 10
w_breadth = 5.

"Narrow casting
object_subclass->w_height = 10.
object_superclass = object_subclass.

CALL METHOD object_superclass->area


EXPORTING
w_length = 10
w_breadth = 10.

"Wide casting
object_superclass ?= object2_superclass.

CALL METHOD object_superclass->area


EXPORTING
w_length = 10
w_breadth = 5.

Output:-
Achieving Multiple Inheritance
By Raja Narayanan

In ABAP, we can achieve Multiple Inheritance through interface.

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 two separate interfaces called ZIF_VECHICLE_MODEL


With the parameter as RV_MODEL

Create interface called ZIF_VECHICLE_DETAIL

With the parameter as RV_DETAIL

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.

Implement the method ZIF_VECHICLE_DETAIL~GET_DETAIL and write your logic.

The similar way we will create another class called ZCL_BIKE_DETAIL which again implement the same
interface’s.

mplement the method ZIF_VECHICLE_MODEL~GET_MODEL and write your logic.


Implement the method ZIF_VECHICLE_DETAIL~GET_DETAIL and write your logic.

Now we will create a new class called ZCL_VECHICLE which will use both the above classes (indirectly
achieving Multiple Inheritance here).

Write the following code in the method GET_VECHICLE


When executing this method we can get the details from both the class.
Creating table of References to invoke methods of different
subclasses
By Ankit Doshi, TCS

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.

ENDCLASS. "animal DEFINITION

*----------------------------------------------------------------------*
* CLASS animal IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS animal IMPLEMENTATION.
METHOD make_noise.
ENDMETHOD. "make_noise

ENDCLASS. "animal IMPLEMENTATION

*----------------------------------------------------------------------*
* CLASS dog DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS dog DEFINITION INHERITING FROM animal.

PUBLIC SECTION.
DATA play TYPE char10.
METHODS make_noise REDEFINITION.

ENDCLASS. "dog DEFINITION

*----------------------------------------------------------------------*
* CLASS dog IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS dog IMPLEMENTATION.

METHOD make_noise.
noise = 'Bark'.
play = 'Wag tail'.
WRITE / : noise,
play.
SKIP.
ENDMETHOD. "make_noise

ENDCLASS. "dog IMPLEMENTATION

*----------------------------------------------------------------------*
* CLASS cat DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cat DEFINITION INHERITING FROM animal.

PUBLIC SECTION.

METHODS make_noise REDEFINITION.

ENDCLASS. "cat DEFINITION


*----------------------------------------------------------------------*
* CLASS cat IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cat IMPLEMENTATION.

METHOD make_noise.
noise = 'Meow'.
WRITE / noise.
SKIP.
ENDMETHOD. "make_noise

ENDCLASS. "cat IMPLEMENTATION

START-OF-SELECTION.

DATA : ref_animal TYPE TABLE OF REF TO animal,


ref_dog TYPE REF TO dog,
ref_cat TYPE REF TO cat.

FIELD-SYMBOLS <fs_animal> LIKE LINE OF ref_animal.


DATA wa_animal LIKE LINE OF ref_animal.

CREATE OBJECT ref_dog.


CREATE OBJECT ref_cat.

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.

Das könnte Ihnen auch gefallen