OOP Notes
OOP Notes
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 pu!_"et#.
PROTECTED SECTION.
METHODS pro_meth.
ENDCLASS.
CLASS lcl_super IMPLEMENTATION.
METHOD pro_meth.
$RITE %& 'Fro" protected "et#od o( lcl_super'.
ENDMETHOD.
METHOD pu!_"et#.
$RITE %& 'Fro" pu!l)c "et#od o( lcl_super'.
ENDMETHOD.
ENDCLASS.
CLASS lcl_su! DEFINITION INHERITIN* FROM lcl_super.
PUBLIC SECTION.
METHODS pu!_"et#&.
ENDCLASS.
CLASS lcl_su! IMPLEMENTATION.
METHOD pu!_"et#&.
$RITE %& 'Fro" pu!l)c "et#od o( lcl_su!'.
+, Protected "et#od o( supercl-ss )s ).#er)ted -.d )s
+, -ccess)!le ). t#e su!cl-ss
pro_meth/ 0.
+, Pu!l)c "et#od o( supercl-ss )s ).#er)ted -.d )s
+, -ccess)!le ). t#e su!cl-ss
pu!_"et#/ 0.
ENDMETHOD.
ENDCLASS.
++++++++++++++++++++++++++++++
START1OF1SELECTION.
DATA su! t2pe re( to lcl_su!.
CREATE OB3ECT su!.
+, A pu!l)c "et#od o( lcl_super )s -lso -ccess)!le outs)de
+, t#e cl-ss' ).#er)t-.ce #)er-rc#2.
su!14pu!_"et#/ 0.
+, A protected "et#od c-..ot !e -ccessed outs)de t#e cl-ss' ).#er)t-.ce #)er-rc#2.
+, He.ce5 t#e code does.'t 6et co"p)led 7)t#out co""e.t).6 t#e !elo7 l).e o( code
su!14pro_meth/ 0.
Storing object references in an internal table:
The code below show how to store the objects references in an internal table.
CLASS lcl_e"p DEFINITION.
PUBLIC SECTION.
METHODS co.structor IMPORTIN* )"c_e.o T8PE )
)"c_e.-"e T8PE str).6
)"c_d.o T8PE )5
s#o7_det-)ls.
PRI9ATE SECTION.
DATA e.o T8PE )5
e.-"e T8PE str).65
d.o T8PE ).
ENDCLASS.
CLASS lcl_e"p IMPLEMENTATION.
METHOD co.structor.
e.o : )"c_e.o.
e.-"e : )"c_e.-"e.
d.o : )"c_d.o.
ENDMETHOD.
METHOD s#o7_det-)ls.
$RITE %; 'ENo'5 e.o5
'EN-"e'5 e.-"e5
'DNo'5 d.o.
ENDMETHOD.
ENDCLASS.
++++++++++++++++++++++++++++++++++++++++++++++++
START1OF1SELECTION.
DATA e< t2pe re( to lcl_e"p5
e& t2pe re( to lcl_e"p5
e; t2pe re( to lcl_e"p5
+, I.ter.-l t-!le ). 7#)c# e-c# ro7 c-. #old - re(ere.ce o( o.e lcl_e"p o!=ect
t_e"p t2pe t-!le o( re( to lcl_e"p5
7_e"p t2pe re( to lcl_e"p.
CREATE OB3ECT e< e>port).6 )"c_e.o : <?<
)"c_e.-"e : '9).-2'
)"c_d.o : <?5
e& e>port).6 )"c_e.o : <?&
)"c_e.-"e : 'A"-r'
)"c_d.o : &?5
e; e>port).6 )"c_e.o : <?;
)"c_e.-"e : 'Pr-deep'
)"c_d.o : ;?.
APPEND e< to t_e"p5
e& to t_e"p5
e; to t_e"p.
LOOP AT t_e"p INTO 7_e"p.
7_e"p14s#o7_det-)ls/ 0.
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_- DEFINITION.
PUBLIC SECTION.
METHODS "et#_-<5
"et#_-&.
ENDCLASS.
CLASS lcl_- IMPLEMENTATION.
METHOD "et#_-<.
$RITE %& 'Fro" lcl_- 1 "et#_-<'.
ENDMETHOD.
METHOD "et#_-&.
$RITE %& 'Fro" lcl_- 1 "et#_-<'.
ENDMETHOD.
ENDCLASS.
*& Subclass
CLASS lcl_! DEFINITION INHERITIN* FROM lcl_-.
PUBLIC SECTION.
METHODS "et#_-& rede@.)t)o.5
"et#_!<.
ENDCLASS.
CLASS lcl_! IMPLEMENTATION.
METHOD "et#_-&.
$RITE %& 'Fro" lcl_! 1 rede@.)t)o. o( "et#_-&'.
ENDMETHOD.
METHOD "et#_!<.
$RITE %& 'Fro" lcl_! 1 "et#_!<'.
ENDMETHOD.
ENDCLASS.
+++++++++++++++++++++++++++++++++++++++++++
START1OF1SELECTION.
DATA - T8PE REF TO lcl_-5
! T8PE REF TO lcl_!.
CREATE OB3ECT -5 !.
-14"et#_-</ 0.
-14"et#_-&/ 0.
SAIP &.
!14"et#_-</ 0.
!14"et#_-&/ 0.
!14"et#_!</ 0.
SAIP &.
+, "oB).6 - su!cl-ss o!=ect re(ere.ce ).to - supercl-ss re(ere.ce B-r)-!le
- : !.
+, -(ter t#e e>ecut)o. o( -!oBe l).e o( code t#e re(1B-r '-' 7#)c# )s o( t2pe
+, lcl_- )s po).t).6 to -. o!=ect o( lcl_!5 7)t# suc# re(ere.ce 7e c-. -ccess
+, o.l2 t#e co"po.e.ts t#-t -re co""o. to !ot# supercl-ss /lcl_-0 -.d t#e
+, su!cl-ss /lcl_!05 t#ou6# t#e -ctu-l o!=ect )s o( t2pe lcl_!
-14"et#_-</ 0.
-14"et#_-&/ 0.
+, T#e code does.'t 6et co"p)led 7)t#out co""e.t).6 t#e !elo7 l).e o( code -s
+, "et#_!< )s -B-)l-!le o.l2 ). t#e su!cl-ss !ut .ot ). t#e supercl-ss
-14"et#_!</ 0.
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. ompile time !or" #tatic polymorphism
$. %untime !or" &ynamic polymorphism
ompile time polymorphism is achieved with method overloading, while runtime polymorphism is
achieved with method overriding. As method overloading is not allowed in A'AP there is no compile time
polymorphism in A'AP.
CLASS lcl_- DEFINITION.
PUBLIC SECTION.
METHODS co.structor IMPORTIN* )"c_> T8PE )5
s#o7_"ess-6e.
PROTECTED SECTION.
DATA > T8PE ).
ENDCLASS. Clcl_- DEFINITION
CLASS lcl_- IMPLEMENTATION.
METHOD co.structor.
> : )"c_>.
ENDMETHOD. Cco.structor
METHOD s#o7_"ess-6e.
$RITE %; 'Fro" cl-ss lcl_-'5 >.
ENDMETHOD. Cs#o7_"ess-6e
ENDCLASS. Clcl_- IMPLEMENTATION
CLASS lcl_! DEFINITION INHERITIN* FROM lcl_-.
PUBLIC SECTION.
METHODS s#o7_"ess-6e REDEFINITION.
ENDCLASS. Clcl_! DEFINITION
CLASS lcl_! IMPLEMENTATION.
METHOD s#o7_"ess-6e.
$RITE %; '111114Fro" cl-ss LCL_B'5 >.
ENDMETHOD. Cs#o7_"ess-6e
ENDCLASS. Clcl_! IMPLEMENTATION
+++++++++++++++++++++++
DATA -< T8PE REF TO lcl_-5
-& T8PE REF TO lcl_-5
-; T8PE REF TO lcl_-5
!< T8PE REF TO lcl_!5
!& T8PE REF TO lcl_!5
t_lcl_- T8PE TABLE OF REF TO lcl_-5
7_- T8PE REF TO lcl_-.
START1OF1SELECTION.
CREATE OB3ECT -< EDPORTIN* )"c_> : <?5
-& EDPORTIN* )"c_> : &?5
-; EDPORTIN* )"c_> : ;?.
CREATE OB3ECT !< EDPORTIN* )"c_> : EF5
!& EDPORTIN* )"c_> : FF.
APPEND -< TO t_lcl_-5
!< TO t_lcl_-5
-& TO t_lcl_-5
!& TO t_lcl_-5
-; TO t_lcl_-.
LOOP AT t_lcl_- INTO 7_-.
+, T#e l).e o( code !elo7 )s 7#ere ru.t)"e pol2"orp#)s" )s ). -ct)o..
+, T#e s-"e re(1B-r '7_-' !e#-Bes d)Gere.lt2 ). e-c# loop )ter-t)o.
+, !2 ).BoH).6 e)t#er supercl-ss )"ple"e.t-t)o. or t#e su!cl-ss
+, )"ple"e.t-t)o. depe.d).6 up t#e o!=ect )t )s po).t).6 to -t t#e ru.t)"e.
7_-14s#o7_"ess-6e/ 0.
ENDLOOP.
Casting:
(p)casting: #toring a subclass object reference in superclass reference variable it is *nown as up)casting.
(p)casting is also referred to as widening a cast.
&own)casting or +arrow)casting: After up)casting, converting the superclass reference bac* to the
subclass type is *nown as down)casting. The downcast assignment operator is ?= .
START1OF1SELECTION.
DATA e< T8PE REF TO lcl_e"p5
ce< T8PE REF TO lcl_co.tr-ct_e"p.
CREATE OB3ECT ce< EDPORTIN* )"c_e.o : <?;
)"c_e.-"e : 'Sud#-'
)"c_d.o : ;?
)"c_#_7-6e : <&?.
+, Upc-st).6 t#e su!cl-ss re(ere.ce t2pe to supercl-ss t2pe
e< : ce<.
+, Cle-r).6 t#e re(ere.ce ). su!cl-ss re(ere.ce B-r)-!le
CLEAR ce<.
+, Do7.c-st).6 t#e supercl-ss re(ere.ce !-cH to su!cl-ss t2pe
ce< ?= e<.
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_- DEFINITION.
PUBLIC SECTION.
METHODS "et#_-< FINAL5
"et#_-&.
ENDCLASS.
,hile 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. 'ut, the method meth_a2 will be allowed to be overriden
as it has not be defined as -.+A/.
CLASS lcl_> DEFINITION FINAL.
PUBLIC SECTION.
METHODS "et#_><5
"et#_>&.
ENDCLASS
Any attempt to define a subclass for lcl_x will result in compile time error.
Predefined class OB!C":
The class OB!C" is the superclass !super)most)class" of all classes in A'AP 0bjects and does not
have any components. .n other words, if a class doesnt inherit from another class, then such class
implicitly inherits from the class 0'12T. .t has the same function for reference variables as the data
type #$% has for normal variables. A reference variable of type 0'12T can hold the reference of any
A'AP object3 however, with such reference we cannot access any of the instance or static components.
CLASS lcl_- DEFINITION.
PUBLIC SECTION.
METHODS "et#od_-<.
ENDCLASS.
CLASS lcl_- IMPLEMENTATION.
METHOD "et#od_-<.
$RITE %; 'Fro" "et#od_-< o( cl-ss lcl_-'.
ENDMETHOD.
ENDCLASS.
++++++++++++++++++++++++++++++++++
START1OF1SELECTION.
DATA o!= T8PE REF TO o!=ect5
-< T8PE REF TO lcl_-.
+, T#e re(ere.ce B-r)-!le o!= c-. #old t#e re(ere.ce o( -. o!=ect o( -.2 cl-ss.
CREATE OB3ECT o!= T8PE lcl_-.
+, $)t# o!= re(ere.ce 7e c-..ot -ccess -.2 t#e correspo.d).6 o!=ect's "et#ods or
+, ot#er co"po.e.ts. U.co""e.t).6 t#e !elo7 l).e o( code 7)ll result ). s2.t-> error
+ o!=14"et#od_-</ 0.
+, To -ccess t#e ).st-.ce "et#od5 do7.1c-st /.-rro71c-st0 to t#e o!=ect's spec)@c cl-ss t2pe
-< I: o!=.
-<14"et#od_-</ 0.