IBM Global Business Services
ABAP Objects Advanced -Inheritance
IBM Corporation 2013
IBM Global Business Services
ABAP Objects Advanced : Inheritance Topics
Concept
Syntax and Visibility
Abstract Classes and Methods
Final Classes and Methods
Static Components in Inheritance
Method Redefinition
Constructor in Inheritance
Object References in Inheritance
Polymorphism through Inheritance
ABAP Objects - Advanced
Jan-2007
IBM Corporation 2013
IBM Global Business Services
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
ABAP Objects - Advanced
Parent Class
Child Class
Multiple Inheritance not
supported by ABAP
Objects
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Generalization
Inheritance : Concept (Contd.)
Root node
Super Class
Class C1
Public:
Protected:
Private:
Generalized
Class
Specialization
Class C2
Specialized
Class
Public:
Protected:
Private:
IS A
Relationship
The relationship between the
base classes and the derived
classes can be represented in an
Inheritance tree. Where base
classes of each derived class can
be retraced along an unique path
to a single root node of the tree.
A Super Class is a Generalization
of its Sub Classes and on the
other hand a Sub Class is a
Specialization of its Super Class.
Class C3
Public:
Protected:
Private:
More specialized class
ABAP Objects - Advanced
Jan-2007
IBM Corporation 2013
IBM Global Business Services
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.
ABAP Objects - Advanced
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Inheritance : Syntax and Visibility
CLASS super_class DEFINITION.
PUBLIC SECTION.
Public components
PROTECTED SECTION.
Protected components
PRIVATE SECTION.
Private components
ENDCLASS.
CLASS sub_class DEFINITION INHERITING FROM
super_class.
PUBLIC SECTION.
Public components
Public components
(super_class)
PROTECTED SECTION.
Protected components
Protected components
(super_class)
The public and protected
components of the Super Class are
visible in the Sub Class.
Private section of the Super Class is
not visible in the Sub Classes.
Private components of the Sub
Class can have same name as the
private component of the Super
Class.
Public and Protected components of
the Sub Class can not have the
same name as that have been used
in Super Class.
PRIVATE SECTION.
Private section
ENDCLASS.
ABAP Objects - Advanced
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Inheritance : Abstract classes and methods
Abstract method
CLASS cl_super DEFINITION.
PUBLIC SECTION.
METHODS: demo1 ABSTRACT,
demo2.
ENDCLASS.
Redefining the Abstract method of
Super Class in the Sub Class
CLASS cl_sub DEFINITION
INHERITING FROM cl_super.
PUBLIC SECTION.
METHODS demo1 REDEFINITION.
A class defined as ABSTRACT cannot be
instantiated , that is one cannot use CREATE
OBJECT with reference to the class.
Abstract class can only be accessed using its
Static Components or its Sub Classes.
Abstract class serves as a template for Sub
Classes
If a class contains any abstract method the
whole class becomes abstract.
A method, which is abstract, should be
redefined in derived class.
An Abstract class can declare and implement
non abstract methods which its Sub Classes
can use with or without redefining them.
ENDCLASS.
7
ABAP Objects - Advanced
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Inheritance : Final classes and methods
CLASS final_class DEFINITION FINAL.
........
ENDCLASS.
CLASS derived_class DEFINITION
INHERITING FROM final_class .
. .. . . . . . . .
ENDCLASS.
CLASS super_class DEFINITION .
........
METHODS final_method FINAL.
ENDCLASS.
A final class can not be inherited
further.
All Methods of a final class are
inherently final and must not be
declared as final in the class
definition.
A final method can not be redefined
further.
If a method of a non-final class is final
then that class can be inherited but
that method cannot be redefined.
CLASS sub_class DEFINITION INHERITING
FROM super_class .
. .. . . . . . . .
METHODS final_method redefinition.
ENDCLASS.
ABAP Objects - Advanced
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Inheritance : Static components
CLASS base_class DEFINITION.
PUBLIC SECTION.
Static component
CLASS-DATA : name TYPE STRING
VALUE ABAP.
..
ENDCLASS.
CLASS derived_class DEFINITION
INHERITING FROM base_class.
. .. . . . . . . .
The public and protected static
components (methods + attributes) of
the Super Class are visible in the Sub
Class.
Public static components can be
accessed through the class
component selector used with any
class in the respective path of
inheritance tree.
ENDCLASS.
START-OF-SELECTION.
base_class=>name.
derived_class=>name.
ABAP Objects - Advanced
Static component is accessed
through class component
selector from the derived class
as well as from the base class
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Inheritance : Method Redefinition
CLASS base_class DEFINITION.
PUBLIC SECTION.
METHODS: meth.
........
ENDCLASS.
CLASS derived_class DEFINITION INHERITING
FROM base_class .
PUBLIC SECTION.
METHODS: meth REDEFINITION.
. .. . . . . . . .
ENDCLASS.
CLASS derived_class IMPLEMENTATION .
METHODS meth.
CLASS METHOD SUPER->meth.
ENDMETHOD.
ENDCLASS.
10
ABAP Objects - Advanced
Redefinition is when the
implementation of an inherited
instance method is changed for the
Sub Class without changing its
signature.
Static methods cant be redefined.
The parameters remain same in the
derived class as it was in base class.
In the redefined method use the
SUPER pseudo reference to access
the original method of the base class.
Redefining the base class method in the
derived class
Calling the Super Class method using
SUPER keyword
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Inheritance : Instance Constructors
CLASS base_class DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING arg1 TYPE STRING.
As all public and protected
components, Sub Class inherits the
constructor method if it is present in
the inheritance tree.
If an object of Sub Class is created
at this time the inherited instance
attributes and private attributes of
the Super Classes must also be
initialized.
As the private attributes of the
Super Class is not visible to Sub
Class, Sub Class cannot fully
initialize its Super Class. Therefore
to ensure complete initialization of
Sub Class and its Super Classes,
constructor is called.
PRIVATE SECTION.
DATA: fld TYPE STRING.
........
Super Class constructor
ENDCLASS.
CLASS derived_class DEFINITION INHERITING FROM
base_class .
PUBLIC SECTION.
METHODS: constructor IMPORTING arg1 TYPE STRING
arg2 TYPE STRING.
PRIVATE SECTION.
DATA: fld TYPE STRING.
........
Sub Class constructor
ENDCLASS.
CLASS derived_class IMPLEMENTATION .
METHODS constructor.
CALL METHOD SUPER->constructor
EXPORTING arg1 = arg1.
fld = arg2 .
ENDMETHOD.
Calling Super Class
constructor
ENDCLASS.
11
ABAP Objects - Advanced
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Inheritance : Instance Constructors (Contd.)
Whether explicit call is required for the instance constructor of the Super Class or
not , when we instantiate an object of Sub Class is depends on the following
conditions:
12
Explicit constructor is
defined in Super
Class?
Explicit constructor is
defined in Sub Class?
Explicit call required?
No
No
No
Yes
No
No
No
Yes
Yes
Yes
Yes
Yes
ABAP Objects - Advanced
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Inheritance : Static Constructors
CLASS base_class DEFINITION.
PUBLIC SECTION.
METHODS: class_constructor.
PRIVATE SECTION.
DATA: fld TYPE STRING.
........
Super Class constructor
ENDCLASS.
CLASS derived_class DEFINITION
INHERITING FROM base_class .
PUBLIC SECTION.
METHODS: class_constructor .
PRIVATE SECTION.
DATA: fld TYPE STRING.
........
Sub Class constructor
ENDCLASS.
CLASS derived_class IMPLEMENTATION .
METHODS class_constructor.
fld = I am sub .
No call for Super Class
constructor
ENDMETHOD.
ENDCLASS.
13
ABAP Objects - Advanced
The redefinition of static constructor
works similarly to instance
constructor.
When an static constructor is
redefined then REDEFINITION
addition is not required.
No parameter interface is possible for
static constructor ( with or without
inheritance)
The runtime environment
automatically ensures that the static
constructors are called in the right
order, so it is not required to call the
static constructor of the Super Class
explicitly.
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Inheritance : Object References
Class C1
Pub: a1, a2
Prot: b1, b2
Priv:c1,c2
DATA oref1 TYPE REF TO C1.
CREATE OBJECT oref1.
Class C2 Inheriting from C1
Pub: a1, a2,a3,a4
DATA oref2 TYPE REF TO C2.
Prot: b1,b2,b3,b4
Priv: c3,c4
CREATE OBJECT oref2.
Class C3 Inheriting from C2
Pub: a1, a2,a3,a4,a5,a6
Prot: b1, b2,b3,b4,b5,b6
Priv:c5,c6
14
ABAP Objects - Advanced
DATA oref3 TYPE REF TO C3.
CREATE OBJECT oref3.
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Inheritance : Object References (Contd.)
Class C1
Pub: a1, a2
Prot: b1, b2
Priv:c1,c2
Class C2 Inheriting from C1
DATA : oref1 TYPE REF TO C1,
oref3 TYPE REF TO C3.
CREATE OBJECT oref3.
oref1 = oref3.
Pub: a1, a2,a3,a4
Prot: b1,b2,b3,b4
Priv: c3,c4
Class C3 Inheriting from C2
Pub: a1, a2,a3,a4,a5,a6
Prot: b1, b2,b3,b4,b5,b6
Priv:c5,c6
15
ABAP Objects - Advanced
oref1 ->a1.
oref1 ->a5.
OK
Error
Reference variables declared with reference to
a Super Class (including a abstract class ) can
point to an object of a Sub Class but can only
access the components of the object that
is known to the Super Class.
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Inheritance : Object References (Contd.)
Class C1
Pub: a1, a2
Prot: b1, b2
Priv:c1,c2
Class C2 Inheriting from C1
DATA : oref1 TYPE REF TO C1,
oref3 TYPE REF TO C3.
Static Type
CREATE OBJECT oref1 TYPE C3.
CREATE OBJECT oref3.
Pub: a1, a2,a3,a4
Prot: b1,b2,b3,b4
Priv: c3,c4
Class C3 Inheriting from C2
Pub: a1, a2,a3,a4,a5,a6
Prot: b1, b2,b3,b4,b5,b6
Dynamic Type
oref1 = oref3.
oref1 ->a1.
OK
oref1 ->a5.
OK
Priv:c5,c6
16
ABAP Objects - Advanced
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Static & dynamic Type for Reference
The Static type of a reference variable:
Is determined using TYPE REF TO
Remains constant through the program run
The Dynamic type of a reference variable:
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.
Static Type for oref1 is C1
CREATE OBJECT oref1 TYPE C2. Dynamic type for oref1 is now C2
CREATE OBJECT oref3.
oref1 = oref3.
17
ABAP Objects - Advanced
Dynamic type for oref1 is now C3
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Inheritance : Narrowing Cast
DATA : oref1 TYPE REF TO Super Class,
oref2 TYPE REF TO Sub Class.
oref1 = oref2.
18
Narrowing Cast
ABAP Objects - Advanced
When static type of the target variable
is more general than the static type of
the source variable, since the dynamic
type of the source variable at run time
can only be more specialized than its
static type, this can be checked during
the syntax check. This is known as
narrowing cast or up cast.
A typical use of narrowing cast is to
prepare for generic access. A user
who is not at all interested in the finer
points of the instances of the Sub
Classes, but wants to access the
generic components for all of them,
could use the Super Class reference to
access all of them in the same way.
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Inheritance : Widening Cast
DATA : oref1 TYPE REF TO Super Class,
oref2 TYPE REF TO Sub Class,
oref3 TYPE REF TO Sub Class.
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
ABAP Objects - Advanced
When static type of the target variable
is more specialized than the static type
of the source variable. This is known
as widening cast or down cast.
The widening cast in the later case
does not cause an error because the
reference oref1 actually points to an
instance in the Sub Class. (oref1 =
oref3)
A typical use for widening cast is when
specific components of instances
need to be addressed and their
references are kept in variables that
are typed on the Super Class.
Jan-2007
IBM Corporation 2013
IBM Global Business Services
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
ABAP Objects - Advanced
Reference variables declared with static
reference to a Super Class can
dynamically point to an object of a Sub
Class of this Super Class and access the
components known to the Super Class.
Methods inherited from Super Class may
be redefined in one or more of the Sub
Classes. This is the basis of
polymorphism using inheritance.
Polymorphism here means using the
same name via same interface to
uniformly address differently
implemented methods, belonging to
different objects of different classes in
the inheritance tree.
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Demonstration
Exercise 5:
Showing how Inheritance works with ABAP object.
Objectives:
Define Sub Classes
Redefine Super Class methods in Sub Classes
21
ABAP Objects - Advanced
Jan-2007
IBM Corporation 2013
IBM Global Business Services
Demonstration
Exercise 6:
In this exercise you will demonstrate Polymorphism through Inheritance.
22
ABAP Objects - Advanced
Jan-2007
IBM Corporation 2013