Академический Документы
Профессиональный Документы
Культура Документы
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
Inheritance : Concept
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
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:
Yes No No
No Yes Yes
Class C1
Pub: a1, a2
DATA oref1 TYPE REF TO C1.
Prot: b1, b2
Priv:c1,c2 CREATE OBJECT oref1.
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.
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.
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.
Demonstration
Exercise 5:
Showing how Inheritance works with ABAP object.
Objectives:
Define Sub Classes
Redefine Super Class methods in Sub Classes
Demonstration
Exercise 6:
In this exercise you will demonstrate Polymorphism through Inheritance.