You are on page 1of 7

Protected Section:

The restriction on the members placed in protected section is that they are not accessible outside the
class inheritance hierarchy, but are accessible within the class in which they are defined and its
subclasses.
REPORT zoop_protected.
CLASS lcl_super DEFINITION.
PUBLIC SECTION.
METHODS: pub_meth.
PROTECTED SECTION.
METHODS: pro_meth.
ENDCLASS.
CLASS lcl_super IMPLEMENTATION.
METHOD pro_meth.
WRITE: /2 'From protected method of lcl_super'.
ENDMETHOD.
METHOD pub_meth.
WRITE: /2 'From public method of lcl_super'.
ENDMETHOD.
ENDCLASS.
CLASS lcl_sub DEFINITION INHERITING FROM lcl_super.
PUBLIC SECTION.
METHODS: pub_meth2.
ENDCLASS.
CLASS lcl_sub IMPLEMENTATION.
METHOD pub_meth2.
WRITE: /2 'From public method of lcl_sub'.
*& Protected method of superclass is inherited and is
*& accessible in the subclass
pro_meth( ).
*& Public method of superclass is inherited and is
*& accessible in the subclass
pub_meth( ).
ENDMETHOD.
ENDCLASS.
******************************
START-OF-SELECTION.
DATA: sub type ref to lcl_sub.
CREATE OBJECT: sub.
*& A public method of lcl_super is also accessible outside
*& the class' inheritance hierarchy.
sub->pub_meth( ).

*& A protected method cannot be accessed outside the class' inheritance hierarchy.
*& Hence, the code doesn't get compiled without commenting the below line of code

sub->pro_meth( ).
Storing object references in an internal table:
The code below show how to store the objects references in an internal table.
CLASS lcl_emp DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING imc_eno TYPE i
imc_ename TYPE string
imc_dno TYPE i,
show_details.
PRIVATE SECTION.
DATA: eno TYPE i,
ename TYPE string,
dno TYPE i.
ENDCLASS.
CLASS lcl_emp IMPLEMENTATION.
METHOD constructor.
eno = imc_eno.
ename = imc_ename.
dno = imc_dno.
ENDMETHOD.
METHOD show_details.
WRITE: /3 'ENo:', eno,
'EName:', ename,
'DNo:', dno.
ENDMETHOD.
ENDCLASS.
************************************************
START-OF-SELECTION.
DATA: e1 type ref to lcl_emp,
e2 type ref to lcl_emp,
e3 type ref to lcl_emp,

*& Internal table in which each row can hold a reference of one lcl_emp object

t_emp type table of ref to lcl_emp,


w_emp type ref to lcl_emp.
CREATE OBJECT: e1 exporting imc_eno = 101
imc_ename = 'Vinay'
imc_dno = 10,
e2 exporting imc_eno = 102
imc_ename = 'Amar'
imc_dno = 20,
e3 exporting imc_eno = 103
imc_ename = 'Pradeep'
imc_dno = 30.
APPEND: e1 to t_emp,
e2 to t_emp,
e3 to t_emp.

LOOP AT t_emp INTO w_emp.


w_emp->show_details( ).
ENDLOOP.
Storing subclass object reference in a superclass refrence variable:
A superclass reference variable can hold the reference of the objects of that class and also that of any of
its subclasses, but the vice versa is not true.
*& Superclass
CLASS lcl_a DEFINITION.
PUBLIC SECTION.
METHODS: meth_a1,
meth_a2.
ENDCLASS.
CLASS lcl_a IMPLEMENTATION.
METHOD meth_a1.
WRITE: /2 'From lcl_a - meth_a1'.
ENDMETHOD.
METHOD meth_a2.
WRITE: /2 'From lcl_a - meth_a1'.
ENDMETHOD.
ENDCLASS.
*& Subclass
CLASS lcl_b DEFINITION INHERITING FROM lcl_a.
PUBLIC SECTION.
METHODS: meth_a2 redefinition,
meth_b1.
ENDCLASS.
CLASS lcl_b IMPLEMENTATION.
METHOD meth_a2.
WRITE: /2 'From lcl_b - redefinition of meth_a2'.
ENDMETHOD.
METHOD meth_b1.
WRITE: /2 'From lcl_b - meth_b1'.
ENDMETHOD.
ENDCLASS.
*******************************************
START-OF-SELECTION.
DATA: a TYPE REF TO lcl_a,
b TYPE REF TO lcl_b.
CREATE OBJECT: a, b.
a->meth_a1( ).
a->meth_a2( ).

SKIP 2.
b->meth_a1( ).
b->meth_a2( ).
b->meth_b1( ).
SKIP 2.
*& moving a subclass object reference into a superclass reference variable
a = b.
*& after the execution of above line of code the ref-var 'a' which is of type
*& lcl_a is pointing to an object of lcl_b, with such reference we can access
*& only the components that are common to both superclass (lcl_a) and the
*& subclass (lcl_b), though the actual object is of type lcl_b
a->meth_a1( ).
a->meth_a2( ).
*& The code doesn't get compiled without commenting the below line of code as
*& meth_b1 is available only in the subclass but not in the superclass
a->meth_b1( ).
Polymorphism:
Poly means many, and morph mean forms or behaviour. The term polymorphism means the same
thing behaving differently in different situations.
Polymorphism is of two types:
1. Compile time (or) Static polymorphism
2. Runtime (or) Dynamic polymorphism
Compile time polymorphism is achieved with method overloading, while runtime polymorphism is
achieved with method overriding. As method overloading is not allowed in ABAP there is no compile time
polymorphism in ABAP.
CLASS lcl_a DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING imc_x TYPE i,
show_message.
PROTECTED SECTION.
DATA: x TYPE i.
ENDCLASS.
"lcl_a DEFINITION
CLASS lcl_a IMPLEMENTATION.
METHOD constructor.
x = imc_x.
ENDMETHOD.
"constructor
METHOD show_message.
WRITE: /3 'From class lcl_a', x.
ENDMETHOD.
"show_message
ENDCLASS.
"lcl_a IMPLEMENTATION
CLASS lcl_b DEFINITION INHERITING FROM lcl_a.
PUBLIC SECTION.
METHODS: show_message REDEFINITION.
ENDCLASS.
"lcl_b DEFINITION

CLASS lcl_b IMPLEMENTATION.


METHOD show_message.
WRITE: /3 '----->From class LCL_B', x.
ENDMETHOD.
"show_message
ENDCLASS.
"lcl_b IMPLEMENTATION
***********************
DATA: a1 TYPE REF TO lcl_a,
a2 TYPE REF TO lcl_a,
a3 TYPE REF TO lcl_a,
b1 TYPE REF TO lcl_b,
b2 TYPE REF TO lcl_b,
t_lcl_a TYPE TABLE OF REF TO lcl_a,
w_a TYPE REF TO lcl_a.
START-OF-SELECTION.
CREATE OBJECT: a1 EXPORTING imc_x = 10,
a2 EXPORTING imc_x = 20,
a3 EXPORTING imc_x = 30.
CREATE OBJECT: b1 EXPORTING imc_x = 45,
b2 EXPORTING imc_x = 55.
APPEND: a1 TO t_lcl_a,
b1 TO t_lcl_a,
a2 TO t_lcl_a,
b2 TO t_lcl_a,
a3 TO t_lcl_a.
LOOP AT t_lcl_a INTO w_a.
*& The line of code below is where runtime polymorphism is in action.
*& The same ref-var 'w_a' behaves differenlty in each loop iteration
*& by invoking either superclass implementation or the subclass
*& implementation depending up the object it is pointing to at the runtime.
w_a->show_message( ).
ENDLOOP.
Casting:
Up-casting: Storing a subclass object reference in superclass reference variable it is known as up-casting.
Up-casting is also referred to as widening a cast.
Down-casting or Narrow-casting: After up-casting, converting the superclass reference back to the
subclass type is known as down-casting. The downcast assignment operator is ?= .
START-OF-SELECTION.
DATA: e1 TYPE REF TO lcl_emp,
ce1 TYPE REF TO lcl_contract_emp.
CREATE OBJECT ce1 EXPORTING imc_eno = 103
imc_ename = 'Sudha'
imc_dno = 30
imc_h_wage = 120.
*& Upcasting the subclass reference type to superclass type
e1 = ce1.

*& Clearing the reference in subclass reference variable


CLEAR ce1.
*& Downcasting the superclass reference back to subclass type
ce1 ?= e1.
Final method and Final class:
A method defined to be final cannot be overriden in the subclasses. A class defined to be final cannot be
subclassed.
CLASS lcl_a DEFINITION.
PUBLIC SECTION.
METHODS: meth_a1 FINAL,
meth_a2.
ENDCLASS.
While creating a subclass of lcl_a, an attempt to override the method meth_a1 in a subclass that inherits
from class lcl_a will result in compile time error. But, the method meth_a2 will be allowed to be overriden
as it has not be defined as FINAL.
CLASS lcl_x DEFINITION FINAL.
PUBLIC SECTION.
METHODS: meth_x1,
meth_x2.
ENDCLASS
Any attempt to define a subclass for lcl_x will result in compile time error.
Predefined class OBJECT:
The class OBJECT is the superclass (super-most-class) of all classes in ABAP Objects and does not
have any components. In other words, if a class doesnt inherit from another class, then such class
implicitly inherits from the class OBJECT. It has the same function for reference variables as the data
type ANY has for normal variables. A reference variable of type OBJECT can hold the reference of any
ABAP object; however, with such reference we cannot access any of the instance or static components.
CLASS lcl_a DEFINITION.
PUBLIC SECTION.
METHODS: method_a1.
ENDCLASS.
CLASS lcl_a IMPLEMENTATION.
METHOD method_a1.
WRITE: /3 'From method_a1 of class lcl_a'.
ENDMETHOD.
ENDCLASS.
**********************************
START-OF-SELECTION.
DATA: obj TYPE REF TO object,
a1 TYPE REF TO lcl_a.
*& The reference variable obj can hold the reference of an object of any class .

CREATE OBJECT obj TYPE lcl_a.

*& With obj reference we cannot access any the corresponding object's methods or
*& other components. Uncommenting the below line of code will result in syntax error

* obj->method_a1( ).
*& To access the instance method, down-cast (narrow-cast) to the object's specific class type

a1 ?= obj.
a1->method_a1( ).