ABAP Objects Advanced - Inheritance: IBM Global Business Services
ABAP Objects Advanced - Inheritance: IBM Global Business Services
Jan-2007
Inheritance : Concept
Inheritance is one of the most powerful feature of object oriented programming.
Inheritance is the process of creating new classes, called derived classes or Sub
Classes or child classes from existing classes called base classes or Super
Classes or parent classes.
The derived class inherits all the capabilities of the base class but can add
embellishments and refinements of its own. The base class is unchanged by this
process.
Grand Parent Class
Parent Class
Parent Class
Child Class
Single Inheritance supported
by ABAP Objects
3
Parent Class
Child Class
Multiple Inheritance not
supported by ABAP
Objects
Jan-2007
Generalization
Super Class
Class C1
Public:
Protected:
Private:
Generalized
Class
Specialization
Class C2
Specialized
Class
Public:
Protected:
Private:
IS A
Relationship
Class C3
Public:
Protected:
Private:
Jan-2007
Inheritance: Advantages
Inheritance permits code Reusability. Reusing existing code removes code
redundancy and saves time & money.
Common components exist only in the Super Class and maintained Centrally.
Once a base class is written and tested, it need not be touched again. This
increases program Reliability.
Jan-2007
PRIVATE SECTION.
Private section
ENDCLASS.
Jan-2007
ENDCLASS.
7
Jan-2007
Jan-2007
Static component
CLASS-DATA : name TYPE STRING
VALUE ABAP.
..
ENDCLASS.
CLASS derived_class DEFINITION
INHERITING FROM base_class.
. .. . . . . . . .
ENDCLASS.
START-OF-SELECTION.
base_class=>name.
derived_class=>name.
Jan-2007
ENDMETHOD.
ENDCLASS.
10
PRIVATE SECTION.
DATA: fld TYPE STRING.
........
ENDCLASS.
ENDCLASS.
CLASS derived_class IMPLEMENTATION .
METHODS constructor.
CALL METHOD SUPER->constructor
EXPORTING arg1 = arg1.
fld = arg2 .
ENDMETHOD.
ENDCLASS.
11
Jan-2007
12
Explicit constructor is
defined in Super
Class?
Explicit constructor is
defined in Sub Class?
No
No
No
Yes
No
No
No
Yes
Yes
Yes
Yes
Yes
Jan-2007
constructor
ENDMETHOD.
ENDCLASS.
13
Jan-2007
Prot: b1,b2,b3,b4
Priv: c3,c4
14
Jan-2007
15
oref1 ->a1.
oref1 ->a5.
OK
Error
Jan-2007
Dynamic Type
oref1 = oref3.
oref1 ->a1.
OK
oref1 ->a5.
OK
Priv:c5,c6
16
Jan-2007
In assignments of reference
variables , static type of a
reference variable is always
more general than the
dynamic type.
Is determined by assignment
Can change during program run
DATA : oref1 TYPE REF TO C1,
oref2 TYPE REF TO C2,
oref3 TYPE REF TO C3.
CREATE OBJECT oref1 TYPE C2. Dynamic type for oref1 is now C2
CREATE OBJECT oref3.
oref1 = oref3.
17
oref1 = oref2.
18
Narrowing Cast
Jan-2007
oref2 = oref1.
Syntax
Error Occurs
oref1 = oref2.
TRY.
oref3 ?= oref1.
Widening Cast
CATCH CX_SY_MOVE_CAST_ERROR.
* React on the Cast Error
ENDTRY.
19
Jan-2007
Inheritance : Polymorphism
CLASS super_class DEFINITION.
..........
METHODS test_method.
ENDCLASS.
CLASS sub_class_one DEFINITION INHERITING
FROM super_class.
.........
METHODS test_method REDEFINTION.
ENDCLASS.
CLASS sub_class_two DEFINITION INHERITING
FROM super_class.
.........
METHODS test_method REDEFINTION.
ENDCLASS.
DATA: supr_obj type ref to super_class,
sub1_obj type ref to sub_class_one,
sub2_obj type ref to sub_class_two.
START-OF-SELECTION.
CREATE OBJECT sub1_obj.
CREATE OBJECT sub2_obj.
supr_obj = sub1_obj.
CALL METHOD supr_obj->test_method.
supr_obj = sub2_obj.
CALL METHOD supr_obj->test_method.
20
Jan-2007
Demonstration
Exercise 5:
Showing how Inheritance works with ABAP object.
Objectives:
Define Sub Classes
Redefine Super Class methods in Sub Classes
21
Jan-2007
Demonstration
Exercise 6:
In this exercise you will demonstrate Polymorphism through Inheritance.
22
Jan-2007