Академический Документы
Профессиональный Документы
Культура Документы
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
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
*& 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( ).