Professional Documents
Culture Documents
Objectives
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
Child Parent
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:
No No No
Yes No No
No Yes Yes
Class C1
Pub: a1, a2
DATA oref TYPE REF TO C1.
Prot: b1, b2
Priv:c1,c2 CREATE OBJECT oref.
Object
DATA : oref1 TYPE REF TO C1,
Class C1
oref3 TYPE REF TO C3.
Pub: a1, a2
Prot: b1, b2
CREATE OBJECT oref3.
Priv:c1,c2
Inheritance : Polymorphism
CLASS super_class DEFINITION.
..........
METHODS test_method. Reference variables declared with static
ENDCLASS. reference to a super class can
CLASS sub_class_one DEFINITION INHERITING
FROM super_class. dynamically point to an object of a
......... subclass of this super class and access
METHODS test_method REDEFINTION.
the components known to the super
ENDCLASS.
CLASS sub_class_two DEFINITION INHERITING class. Methods inherited from super
FROM super_class. class may be redefined in one or more of
.........
METHODS test_method REDEFINTION. the subclasses. This is the basis of
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.
START-OF-SELECTION.
address differently implemented
CREATE OBJECT sub1_obj. methods, belonging to different objects
CREATE OBJECT sub2_obj. of different classes in the inheritance
supr_obj = sub1_obj.
CALL METHOD supr_obj->test_method.
tree.
supr_obj = sub2_obj.
CALL METHOD supr_obj->test_method.
Demonstration
Practice
Concept
Defining Interfaces
Implementing Interface in Classes
Compound Interfaces
Alias Names
Interface References
Polymorphism through interfaces
Interface and Inheritance
Interfaces : Concepts
Like Java, ABAP Objects does not
Interface I1 OBJECT
permit multiple inheritance, but using
DATA: interfaces with inheritance we can
CLASS-DATA: Class C1 achieve the same.
METHODS: Public: Interface is similar to abstract class
CLASS-METHODS: Interfaces I1 but it has only definitions part.
EVENTS: The interface can be implemented
CLASS-EVENTS: only in the class that uses it.
Class C2
Interface, which is an independent
Public: structure, is used to implement in a
Interfaces I1 class to extend the scope of a class.
Interface allows users to address
Class C3 different classes via a universal
point of contact.
Public:
Interfaces I1
CLASS C1 IMPLEMENTATION.
METHOD I1~m1. START-OF-SELECTION.
... DATA : oref TYPE REF TO c1
ENDMETHOD. CREATE OBJECT oref.
CALL METHOD : oref->I2~meth1.
METHOD : I2~m2.
CALL METHOD : oref->meth2 .
...
ENDMETHOD.
ENDCLASS.
Interfaces : Polymorphism
INTERFACE I1.
METHODS : M1 .
ENDINTERFACE.
Interfaces allow to use different
CLASS C1 DEFINITION.
PUBLIC SECTION.
classes in a uniform way using
INTERFACES : I1. interface references. Any no of class
ENDCLASS.
can implement the interface
CLASS C1 IMPLEMENTATION.
METHOD I1~M1.
differently.
….
ENDMETHOD. The identical interface reference
ENDCLASS.
CLASS C2 DEFINITION.
variable, statically typed to this
PUBLIC SECTION. interface, operates on multiple
INTERFACES : I1.
ENDCLASS. objects that implement the interface.
CLASS C2 IMPLEMENTATION.
METHOD I1~M1.
….
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA : OREF1 TYPE REF TO C1 ,
OREF2 TYPE REF TO C2 ,
IREF TYPE REF TO I1 .
CREATE OBJECT : OREF1 ,
OREF2 .
IREF = OREF1.
CALL METHOD IREF->M1.
IREF = OREF2.
CALL METHOD IREF->M1.
Public:
Interfaces I1
Class C3
Public:
Interfaces I1
Demonstration
Practice
Friends
CLASS c1 DEFINITION FRIENDS With the FRIEND relationship one
obj1…objn. class can grant another class or
interface ( and all classes that
CLASS c1 DEFINITION LOCAL FRIENDS implement that interface ) access to
obj1…objn. its PROTECTED and PRIVATE
components.
CLASS c1 DEFINITION GLOBAL Friendship is one sided: class c1
FRIENDS obj1…objn. granting friendship to class c2 does
not mean c1 is a friend of c2 unless
c2 explicitly grants c1 as friend.
Subclasses of friend and interfaces
that receives a friend as a
component interface become friend.
A friend of a super class is not
automatically friend of its sub class
Demonstration
Practice
Events : Concept
Class C1 Class C2
Publish and
Subscribe
Raises Handles
Mehtods:trigger_e1 Methods:
event e1 event e1
Events: e1 handler_e1
Each object or class that can trigger instance or static events has an invisible
handler table, where entries occur as a result of SET HANDLER statement.
The RAISE EVENT statement in the triggering method interrupts the
execution of the method until the run time finishes cascading through each
handler registered in the handler table . The initial triggering method then
resume execution.
To delete single entries from the handler table use the ACTIVATION addition
of the SET HANDLER statement.
39 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation
IBM Global Services
Demonstration
Creating an Event in a class, triggering that event and handling that event from
another class.
Practice
Creating an Event in a class, triggering that event and handling that event from
another class.
Concept
Demonstrating How to create Global class.
Global classes and interfaces are created and stored in class pool and interface
pool like function pool (function group).
Global classes and interfaces are managed centrally and available to every
program.
Custom Global classes and interfaces must begin with Y* or Z*.
Global and local classes and interfaces have the same components, and there is
no difference in how they are used within programs.
9 Macros
9
10
Local class
Types
Demonstration
Practice
Summary
Summary (Contd.)
With FRIEND relationship one class can grant another class/interface (and all
classes that implemented the interface) access to its PROTECTED and PRIVATE
components.
Questions
What is an Event?
What statement we have to use to register an event handler for a specific Event?
What is Inheritance?
How can we achieve multiple Inheritance in ABAP?
How can we access nested interface?
What is a Friend class?
Which transaction we use to create Global class?