Вы находитесь на странице: 1из 22

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

2 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

Parent Class Child Class

Child Class Multiple Inheritance not


supported by ABAP
Single Inheritance supported Objects
by ABAP Objects

3 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Inheritance : Concept (Contd.)

Root node Super Class The relationship between the


Generalization

base classes and the derived


classes can be represented in an
Class C1
Inheritance tree. Where base
Generalized Public: classes of each derived class can
Class Protected: be retraced along an unique path
Private: to a single root node of the tree.
A Super Class is a Generalization
Class C2
of its Sub Classes and on the
Public: other hand a Sub Class is a
Specialized
Class Protected: Specialization of its Super Class.
IS A
Specialization

Private: Relationship

Class C3
Public:
Protected: More specialized class
Private:
4 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.

5 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Inheritance : Syntax and Visibility


CLASS super_class DEFINITION.
PUBLIC SECTION. The public and protected
Public components components of the Super Class are
PROTECTED SECTION.
visible in the Sub Class.
Protected components
PRIVATE SECTION. Private section of the Super Class is
Private components not visible in the Sub Classes.
ENDCLASS.
Private components of the Sub
CLASS sub_class DEFINITION INHERITING FROM
super_class. Class can have same name as the
PUBLIC SECTION. private component of the Super
Public components Class.
Public components
(super_class) Public and Protected components of
PROTECTED SECTION. the Sub Class can not have the
Protected components same name as that have been used
Protected components
(super_class)
in Super Class.
PRIVATE SECTION.
Private section
ENDCLASS.

6 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Inheritance : Abstract classes and methods

Abstract method A class defined as ABSTRACT cannot be


instantiated , that is one cannot use CREATE
CLASS cl_super DEFINITION. OBJECT with reference to the class.
PUBLIC SECTION. Abstract class can only be accessed using its
METHODS: demo1 ABSTRACT, Static Components or its Sub Classes.
demo2. Abstract class serves as a template for Sub
ENDCLASS. Classes
If a class contains any abstract method the
Redefining the Abstract method of whole class becomes abstract.
Super Class in the Sub Class
A method, which is abstract, should be
CLASS cl_sub DEFINITION redefined in derived class.
INHERITING FROM cl_super. An Abstract class can declare and implement
PUBLIC SECTION. non abstract methods which its Sub Classes
METHODS demo1 REDEFINITION. 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. A final class can not be inherited


........ further.
ENDCLASS.
All Methods of a final class are
CLASS derived_class DEFINITION INHERITING
FROM final_class .
inherently final and must not be
. .. . . . . . . .
declared as final in the class
ENDCLASS.
definition.
A final method can not be redefined
CLASS super_class DEFINITION . further.
........ If a method of a non-final class is final
METHODS final_method FINAL. then that class can be inherited but
ENDCLASS. that method cannot be redefined.
CLASS sub_class DEFINITION INHERITING
FROM super_class .
. .. . . . . . . .
METHODS final_method redefinition.
ENDCLASS.

8 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Inheritance : Static components

CLASS base_class DEFINITION.


The public and protected static
PUBLIC SECTION. Static component components (methods + attributes) of
CLASS-DATA : name TYPE STRING the Super Class are visible in the Sub
VALUE ABAP. Class.
.. Public static components can be
ENDCLASS. accessed through the class
CLASS derived_class DEFINITION component selector used with any
INHERITING FROM base_class. class in the respective path of
. .. . . . . . . . inheritance tree.
ENDCLASS.

Static component is accessed


START-OF-SELECTION. through class component
base_class=>name. selector from the derived class
derived_class=>name. as well as from the base class

9 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Inheritance : Method Redefinition

CLASS base_class DEFINITION. Redefinition is when the


PUBLIC SECTION. implementation of an inherited
METHODS: meth. instance method is changed for the
........ Sub Class without changing its
ENDCLASS. signature.
CLASS derived_class DEFINITION INHERITING Static methods cant be redefined.
FROM base_class .
PUBLIC SECTION.
The parameters remain same in the
METHODS: meth REDEFINITION.
derived class as it was in base class.
. .. . . . . . . . In the redefined method use the
ENDCLASS. SUPER pseudo reference to access
the original method of the base class.
CLASS derived_class IMPLEMENTATION .
METHODS meth. Redefining the base class method in the
CLASS METHOD SUPER->meth. derived class

ENDMETHOD. Calling the Super Class method using
ENDCLASS. SUPER keyword

10 ABAP Objects - Advanced 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
PRIVATE SECTION. components, Sub Class inherits the
DATA: fld TYPE STRING.
........
constructor method if it is present in
Super Class constructor the inheritance tree.
ENDCLASS.
CLASS derived_class DEFINITION INHERITING FROM
base_class .
If an object of Sub Class is created
PUBLIC SECTION. at this time the inherited instance
METHODS: constructor IMPORTING arg1 TYPE STRING attributes and private attributes of
arg2 TYPE STRING. the Super Classes must also be
PRIVATE SECTION.
initialized.
DATA: fld TYPE STRING. Sub Class constructor
........ As the private attributes of the
ENDCLASS. Super Class is not visible to Sub
CLASS derived_class IMPLEMENTATION .
Class, Sub Class cannot fully
METHODS constructor.
CALL METHOD SUPER->constructor
initialize its Super Class. Therefore
EXPORTING arg1 = arg1. to ensure complete initialization of
fld = arg2 . Sub Class and its Super Classes,

Calling Super Class
constructor is called.
ENDMETHOD. 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:

Explicit constructor is Explicit constructor is Explicit call required?


defined in Super defined in Sub Class?
Class?
No No No

Yes No No

No Yes Yes

Yes Yes Yes

12 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Inheritance : Static Constructors


CLASS base_class DEFINITION.
PUBLIC SECTION. The redefinition of static constructor
METHODS: class_constructor. works similarly to instance
PRIVATE SECTION.
constructor.
DATA: fld TYPE STRING.
........ Super Class constructor When an static constructor is
ENDCLASS. redefined then REDEFINITION
CLASS derived_class DEFINITION addition is not required.
INHERITING FROM base_class .
PUBLIC SECTION. No parameter interface is possible for
METHODS: class_constructor . static constructor ( with or without
PRIVATE SECTION. inheritance)
DATA: fld TYPE STRING.
........
The runtime environment
Sub Class constructor
ENDCLASS. automatically ensures that the static
CLASS derived_class IMPLEMENTATION . constructors are called in the right
METHODS class_constructor. order, so it is not required to call the
fld = I am sub . static constructor of the Super Class
No call for Super Class
explicitly.
constructor
ENDMETHOD.
ENDCLASS.

13 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Inheritance : Object References

Class C1

Pub: a1, a2
DATA oref1 TYPE REF TO C1.
Prot: b1, b2
Priv:c1,c2 CREATE OBJECT oref1.

Class C2 Inheriting from C1

Pub: a1, a2,a3,a4 DATA oref2 TYPE REF TO C2.


Prot: b1,b2,b3,b4
CREATE OBJECT oref2.
Priv: c3,c4

Class C3 Inheriting from C2


Pub: a1, a2,a3,a4,a5,a6 DATA oref3 TYPE REF TO C3.
Prot: b1, b2,b3,b4,b5,b6
CREATE OBJECT oref3.
Priv:c5,c6

14 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Inheritance : Object References (Contd.)

Class C1 DATA : oref1 TYPE REF TO C1,


Pub: a1, a2 oref3 TYPE REF TO C3.
Prot: b1, b2
Priv:c1,c2
CREATE OBJECT oref3.
Class C2 Inheriting from C1 oref1 = oref3.

Pub: a1, a2,a3,a4


Prot: b1,b2,b3,b4 oref1 ->a1. OK
Priv: c3,c4 oref1 ->a5. Error

Class C3 Inheriting from C2 Reference variables declared with reference to


Pub: a1, a2,a3,a4,a5,a6 a Super Class (including a abstract class ) can
Prot: b1, b2,b3,b4,b5,b6 point to an object of a Sub Class but can only
access the components of the object that
Priv:c5,c6
is known to the Super Class.

15 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Inheritance : Object References (Contd.)

Class C1 DATA : oref1 TYPE REF TO C1,


Pub: a1, a2 oref3 TYPE REF TO C3.
Prot: b1, b2 Static Type
Priv:c1,c2
CREATE OBJECT oref1 TYPE C3.
Class C2 Inheriting from C1 CREATE OBJECT oref3.

Pub: a1, a2,a3,a4 Dynamic Type


Prot: b1,b2,b3,b4 oref1 = oref3.
Priv: c3,c4
oref1 ->a1. OK
Class C3 Inheriting from C2
Pub: a1, a2,a3,a4,a5,a6
oref1 ->a5. OK
Prot: b1, b2,b3,b4,b5,b6
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: In assignments of reference


Is determined using TYPE REF TO variables , static type of a
Remains constant through the program run reference variable is always
more general than the
dynamic type.
The Dynamic type of a reference variable:
Is determined by assignment
Can change during program run

DATA : oref1 TYPE REF TO C1, Static Type for oref1 is 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. Dynamic type for oref1 is now C3

17 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Inheritance : Narrowing Cast

DATA : oref1 TYPE REF TO Super Class, When static type of the target variable
oref2 TYPE REF TO Sub Class. 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
oref1 = oref2. Narrowing Cast 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.

18 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Inheritance : Widening Cast

DATA : oref1 TYPE REF TO Super Class, When static type of the target variable
oref2 TYPE REF TO Sub Class, is more specialized than the static type
oref3 TYPE REF TO Sub Class. of the source variable. This is known
as widening cast or down cast.
The widening cast in the later case
Syntax does not cause an error because the
oref2 = oref1.
Error Occurs reference oref1 actually points to an
oref1 = oref2. instance in the Sub Class. (oref1 =
oref3)
TRY. A typical use for widening cast is when
oref3 ?= oref1. Widening Cast specific components of instances
need to be addressed and their
CATCH CX_SY_MOVE_CAST_ERROR. references are kept in variables that
* React on the Cast Error are typed on the Super Class.
ENDTRY.

19 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Inheritance : Polymorphism
CLASS super_class DEFINITION.
..........
Reference variables declared with static
METHODS test_method.
ENDCLASS. reference to a Super Class can
CLASS sub_class_one DEFINITION INHERITING dynamically point to an object of a Sub
FROM super_class.
......... Class of this Super Class and access the
METHODS test_method REDEFINTION. components known to the Super Class.
ENDCLASS. Methods inherited from Super Class may
CLASS sub_class_two DEFINITION INHERITING
FROM super_class. be redefined in one or more of the Sub
......... Classes. This is the basis of
METHODS test_method REDEFINTION.
ENDCLASS.
polymorphism using inheritance.

DATA: supr_obj type ref to super_class,


Polymorphism here means using the
sub1_obj type ref to sub_class_one, same name via same interface to
sub2_obj type ref to sub_class_two. uniformly address differently
START-OF-SELECTION.
implemented methods, belonging to
CREATE OBJECT sub1_obj.
CREATE OBJECT sub2_obj. different objects of different classes in
supr_obj = sub1_obj. the inheritance tree.
CALL METHOD supr_obj->test_method.
supr_obj = sub2_obj.
CALL METHOD supr_obj->test_method.

20 ABAP Objects - Advanced 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

Вам также может понравиться