Documente Academic
Documente Profesional
Documente Cultură
-1-
Objectives
Recognize the concept of Object Oriented Programming (OOP)
Identify the features of Object Oriented Programming
Analyze the basic building blocks of ABAP Objects
Inheritance
Events
Define Global class
Persistent Class
Exception Class
-2-
-3-
Objects
Objects are instances of the class.
Ecapsulation
Hiding data and its related logic behind well defined interfaces.
Inheritance
Reusing attributes and methods while allowing for specialization.
Polymorphism
Simplifying by hiding varying implementations behind the same interface.
-4-
-5-
Local Classes
Accessed from ?
Any Program
Where store ?
Tools required to
create ?
Namespace ?
Any
-6-
METHODS
For instance methods
EVENTS
For instance events
Static components:
CLASS-DATA
For static attributes
CLASS-METHODS
For static methods
CLASS-EVENTS
For static events
-8-
Visibility of Components
All components of a class must belong to a visibility section. Components can be
public, protected or private.
Public components form the external interface of the class they are visible to all
users of the class as well as to methods within the class and to methods of
subclasses.
Protected components form the interface of the class to its subclasses they are
visible to methods of the heirs of the class as well as to methods within the class.
Private components can only be used in the methods of the class itself.
-9-
Methods
ABAP codes are written within a method to
incorporate the functionality.
PRIVATE SECTION.
DATA:
ENDCLASS.
ENDMETHOD.
ENDCLASS.
- 10 -
- 11 -
Multiple instantiation
CLASS test DEFINITION.
PUBLIC SECTION.
METHODS meth.
ENDCLASS.
ENDCLASS.
DATA: oref1 TYPE REF TO test,
oref2 TYPE REF TO test.
START-OF-SELECTION.
CREATE OBJECT oref1, oref2.
- 12 -
Pointer tables
DATA: oref1 TYPE REF TO test,
oref2 TYPE REF TO test,
oref3 TYPE REF TO test.
DATA: oref TYPE REF TO test,
oref_tab TYPE TABLE OF REF TO test.
START-OF-SELECTION.
DO 3 TIMES.
CREATE OBJECT oref.
APPEND oref TO oref_tab.
ENDDO.
LOOP AT oref_tab INTO oref.
CALL METHOD oref->meth.
ENDLOOP.
- 13 -
Constructors
METHODS constructor
IMPORTING
Instance constructor
CLASS Test DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING A TYPE I.
PRIVATE SECTION.
DATA: fld TYPE STRING.
ENDCLASS.
Inheritance Topics
Concept
Syntax and Visibility
Object References in Inheritance
Method Redefinition
Abstract Classes and Methods
Final Classes and Methods
Constructor in Inheritance
Polymorphism through Inheritance
- 15 -
Inheritance - Concept
The concept of inheritance is used to incorporate the properties of an existing
class into a new class. The beauty of this feature is that the methods and the
attribute of the existing class need not to be coded into the new class, as well as
new features can be added into the new class.
Inheritance can be single inheritance ( subclass is inherited from a single super
class) or multiple inheritance (subclass is inherited from multiple super classes).
But ABAP Objects supports only single inheritance.
Parent
Parent
Grand Parent
Parent
Child
Multiple Inheritance not
supported by ABAP Objects
Child
- 16 -
Protected components_a
PRIVATE SECTION.
Private components
ENDCLASS.
CLASS sub_class_b DEFINITION INHERITING FROM
super_class_a.
PUBLIC SECTION.
Public components_b
Public components (super_class_a)
PROTECTED SECTION.
Protected components_b
Protected components (super_class_a)
PRIVATE SECTION.
Private section
ENDCLASS.
- 17 -
PUBLIC SECTION.
METHODS: meth REDEFINITION.
. .. . . . . . . .
ENDCLASS.
CLASS derived_class IMPLEMENTATION .
METHODS meth.
CALL METHOD SUPER->meth.
ENDMETHOD.
ENDCLASS.
Abstract method
CLASS cl_super DEFINITION.
PUBLIC SECTION.
METHODS: demo1 ABSTRACT,
demo2.
ENDCLASS.
Redefining the Abstract method of Super class
in the Sub class
PUBLIC SECTION.
METHODS demo1 REDEFINITION.
ENDCLASS.
- 20 -
ENDCLASS.
CLASS derived_class DEFINITION INHERITING
FROM final_class .
. .. . . . . . . .
ENDCLASS.
CLASS super_class DEFINITION .
........
METHODS final_method
final_methodFINAL.
FINAL.
ENDCLASS.
CLASS sub_class DEFINITION INHERITING
FROM super_class .
. .. . . . . . . .
METHODS final_method
final_methodredefinition.
redefinition.
ENDCLASS.
- 21 -
Inheritance - Constructors
CLASS base_class DEFINITION.
PUBLIC SECTION.
* Super class constructor
METHODS: constructor IMPORTING arg1 TYPE
STRING.
PRIVATE SECTION.
DATA: fld TYPE STRING.
ENDCLASS.
CLASS derived_class DEFINITION INHERITING
FROM base_class .
PUBLIC SECTION.
* Sub class constructor
METHODS: constructor IMPORTING arg1 TYPE
STRING
arg2 TYPE
STRING.
PRIVATE SECTION.
DATA: fld TYPE STRING.
ENDCLASS.
CLASS derived_class IMPLEMENTATION .
METHODS constructor.
* Calling super class constructor
CALL METHOD SUPER->constructor
EXPORTING arg1 = arg1.
fld = arg2 .
ENDMETHOD.
ENDCLASS.
- 22 -
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.
- 23 -
Interfaces - Concepts
Interface is similar to abstract class.
It has only definitions part.
The interface can be implemented only in the class that uses it.
Interface, which is an independent structure, is used to implement in a class to
extend the scope of a class.
- 24 -
- 25 -
CLASS interface_class
IMPLEMENTATION.
METHOD
My_interface~my_interface_metho
d.
DATA: num TYPE I VALUE 10.
Write:/ num.
ENDMETHOD.
ENDCLASS.
* Event declaration
EVENTS
My_event_click.
METHODS : event_raising_method.
ENDCLASS.
METHOD event_raising_method.
*
Triggering the event
RAISE EVENT My_event_click.
ENDMETHOD.
ENDCLASS.
- 27 -
Handling Events
CLASS test DEFINITION.
PUBLIC SECTION.
* Declaration for event handler method
METHODS: event_method_click
FOR EVENT My_event_click
OF
test_event_class.
ENDCLASS.
CLASS test IMPLEMENTATION.
METHOD event_method_click.
..
ENDMETHOD.
ENDCLASS.
- 28 -
Registering events
START-OF-SELECTION.
DATA: object1 TYPE REF TO test,
object2 TYPE REF TO
test_event_class.
CREATE OBJECT: object1,
object2.
- 29 -
- 30 -
- 31 -
- 32 -
- 33 -
- 34 -
Persistence Services
- 35 -
- 36 -
- 37 -
- 38 -
- 39 -
- 41 -
- 42 -
- 43 -