03 abap objects -inheritance

22
© IBM Corporation 2013 IBM Global Business Services ABAP Objects Advanced - Inheritance

Upload: bakkalibilal

Post on 24-Dec-2015

87 views

Category:

Documents


16 download

DESCRIPTION

03 ABAP Objects -Inheritance

TRANSCRIPT

© IBM Corporation 2013

IBM Global Business Services

ABAP Objects Advanced -Inheritance

IBM Global Business Services

© IBM Corporation 20132 Jan-2007ABAP Objects - Advanced

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

IBM Global Business Services

© IBM Corporation 20133 Jan-2007ABAP Objects - Advanced

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

Child Class

Single Inheritance supported by ABAP Objects

Parent Class Parent Class

Child Class

Multiple Inheritance not supported by ABAP

Objects

IBM Global Business Services

© IBM Corporation 20134 Jan-2007ABAP Objects - Advanced

Inheritance : Concept (Contd.)

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.

Super Class

Public:

Protected:

Private:

Class C1

Public:

Protected:

Private:

Class C2

Public:

Protected:

Private:

Class C3

Root node

GeneralizedClass

Specialized Class

More specialized class

Gen

eral

izat

ion

Sp

ecia

lizat

ion “IS A”

Relationship

IBM Global Business Services

© IBM Corporation 20135 Jan-2007ABAP Objects - Advanced

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.

IBM Global Business Services

© IBM Corporation 20136 Jan-2007ABAP Objects - Advanced

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)

PRIVATE SECTION.

Private section

ENDCLASS.

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.

IBM Global Business Services

© IBM Corporation 20137 Jan-2007ABAP Objects - Advanced

Inheritance : Abstract classes and methods

CLASS cl_super DEFINITION.

PUBLIC SECTION.

METHODS: demo1 ABSTRACT,

demo2.

ENDCLASS.

CLASS cl_sub DEFINITION INHERITING FROM cl_super.

PUBLIC SECTION.

METHODS demo1 REDEFINITION.

ENDCLASS.

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.

Redefining the Abstract method of Super Class in the Sub Class

Abstract method

IBM Global Business Services

© IBM Corporation 20138 Jan-2007ABAP Objects - Advanced

Inheritance : Final classes and methods

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 final_class DEFINITION FINAL.

. . . . . . . .

ENDCLASS.

CLASS derived_class DEFINITION INHERITING FROM final_class .

. .. . . . . . . .

ENDCLASS.

CLASS super_class DEFINITION .

. . . . . . . .

METHODS final_method FINAL.

ENDCLASS.

CLASS sub_class DEFINITION INHERITING FROM super_class .

. .. . . . . . . .

METHODS final_method redefinition.

ENDCLASS.

IBM Global Business Services

© IBM Corporation 20139 Jan-2007ABAP Objects - Advanced

Inheritance : Static components

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.

CLASS base_class DEFINITION.

PUBLIC SECTION.

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.

Static component

Static component is accessed through class component selector from the derived class as well as from the base class

IBM Global Business Services

© IBM Corporation 201310 Jan-2007ABAP Objects - Advanced

Inheritance : Method Redefinition

Redefinition is when the implementation of an inherited instance method is changed for the Sub Class without changing its signature.

Static methods can’t 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.

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.

Redefining the base class method in the derived class

Calling the Super Class method using SUPER keyword

IBM Global Business Services

© IBM Corporation 201311 Jan-2007ABAP Objects - Advanced

Inheritance : Instance Constructors

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.

CLASS base_class DEFINITION.

PUBLIC SECTION.

METHODS: constructor IMPORTING arg1 TYPE STRING.

PRIVATE SECTION.

DATA: fld TYPE STRING.

. . . . . . . .

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.

. . . . . . . .

ENDCLASS.

CLASS derived_class IMPLEMENTATION .

METHODS constructor.

CALL METHOD SUPER->constructor

EXPORTING arg1 = arg1.

fld = arg2 .

………

ENDMETHOD.

ENDCLASS.

Super Class constructor

Sub Class constructor

Calling Super Class constructor

IBM Global Business Services

© IBM Corporation 201312 Jan-2007ABAP Objects - Advanced

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 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

IBM Global Business Services

© IBM Corporation 201313 Jan-2007ABAP Objects - Advanced

Inheritance : Static Constructors

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.

CLASS base_class DEFINITION. PUBLIC SECTION. METHODS: class_constructor. PRIVATE SECTION. DATA: fld TYPE STRING. . . . . . . . .ENDCLASS.CLASS derived_class DEFINITION INHERITING FROM base_class . PUBLIC SECTION. METHODS: class_constructor . PRIVATE SECTION. DATA: fld TYPE STRING. . . . . . . . .ENDCLASS.CLASS derived_class IMPLEMENTATION . METHODS class_constructor. fld = ‘ I am sub’ . ……… ENDMETHOD.ENDCLASS.

Super Class constructor

Sub Class constructor

No call for Super Class constructor

IBM Global Business Services

© IBM Corporation 201314 Jan-2007ABAP Objects - Advanced

Inheritance : Object References

Pub: a1, a2

Prot: b1, b2

Priv:c1,c2

Class C1

Pub: a1, a2,a3,a4

Prot: b1,b2,b3,b4

Priv: c3,c4

Class C2 Inheriting from C1

Pub: a1, a2,a3,a4,a5,a6

Prot: b1, b2,b3,b4,b5,b6

Priv:c5,c6

DATA oref1 TYPE REF TO C1.

CREATE OBJECT oref1.

DATA oref2 TYPE REF TO C2.

CREATE OBJECT oref2.

DATA oref3 TYPE REF TO C3.

CREATE OBJECT oref3.

Class C3 Inheriting from C2

IBM Global Business Services

© IBM Corporation 201315 Jan-2007ABAP Objects - Advanced

Inheritance : Object References (Contd.)

DATA : oref1 TYPE REF TO C1,

oref3 TYPE REF TO C3.

CREATE OBJECT oref3.

oref1 = oref3.

oref1 ->a1.

oref1 ->a5.

OK

Error

Pub: a1, a2

Prot: b1, b2

Priv:c1,c2

Class C1

Pub: a1, a2,a3,a4

Prot: b1,b2,b3,b4

Priv: c3,c4

Class C2 Inheriting from C1

Pub: a1, a2,a3,a4,a5,a6

Prot: b1, b2,b3,b4,b5,b6

Priv:c5,c6

Class C3 Inheriting from C2 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.

IBM Global Business Services

© IBM Corporation 201316 Jan-2007ABAP Objects - Advanced

Inheritance : Object References (Contd.)

DATA : oref1 TYPE REF TO C1,

oref3 TYPE REF TO C3.

CREATE OBJECT oref1 TYPE C3.

CREATE OBJECT oref3.

oref1 = oref3.

oref1 ->a1.

oref1 ->a5.

OK

OK

Pub: a1, a2

Prot: b1, b2

Priv:c1,c2

Class C1

Pub: a1, a2,a3,a4

Prot: b1,b2,b3,b4

Priv: c3,c4

Class C2 Inheriting from C1

Pub: a1, a2,a3,a4,a5,a6

Prot: b1, b2,b3,b4,b5,b6

Priv:c5,c6

Class C3 Inheriting from C2

Dynamic Type

Static Type

IBM Global Business Services

© IBM Corporation 201317 Jan-2007ABAP Objects - Advanced

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: 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.

CREATE OBJECT oref3.

oref1 = oref3.

Static Type for oref1 is C1

Dynamic type for oref1 is now C2

Dynamic type for oref1 is now C3

In assignments of reference variables , static type of a reference variable is always more general than the dynamic type.

IBM Global Business Services

© IBM Corporation 201318 Jan-2007ABAP Objects - Advanced

Inheritance : Narrowing Cast

DATA : oref1 TYPE REF TO Super Class,

oref2 TYPE REF TO Sub Class.

oref1 = oref2.

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.

Narrowing Cast

IBM Global Business Services

© IBM Corporation 201319 Jan-2007ABAP Objects - Advanced

Inheritance : Widening Cast

DATA : oref1 TYPE REF TO Super Class,

oref2 TYPE REF TO Sub Class,

oref3 TYPE REF TO Sub Class.

oref2 = oref1.

oref1 = oref2.

TRY.

oref3 ?= oref1.

CATCH CX_SY_MOVE_CAST_ERROR.

* React on the Cast Error

ENDTRY.

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.

SyntaxError Occurs

Widening Cast

IBM Global Business Services

© IBM Corporation 201320 Jan-2007ABAP Objects - Advanced

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.

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.

IBM Global Business Services

© IBM Corporation 201321 Jan-2007ABAP Objects - Advanced

Demonstration

Exercise 5:

Showing how Inheritance works with ABAP object.Objectives:

Define Sub Classes

Redefine Super Class methods in Sub Classes

IBM Global Business Services

© IBM Corporation 201322 Jan-2007ABAP Objects - Advanced

Demonstration

Exercise 6:

In this exercise you will demonstrate Polymorphism through Inheritance.