Escolar Documentos
Profissional Documentos
Cultura Documentos
This object is a
box
Outside color is
blue (public)
Functions of the
Box??
(methods)
It can store things
It can occupy space
inside color is
red (private)
What is the status of
the box ? (events)
The box is semi open
Global Classes
Local Classes
Can be accessed
from
Any program
Where Stored
In the Class
Repository
In the program
where it is defined
Where Created
Namespace
Any
Declaring a class.
A class declaration has two parts.
Definition
Implementation
Example.
CLASS test DEFINITION.
<class body>
ENDCLASS.
CLASS test IMPLEMENTATION.
<class body>
{method implementation is done here}
ENDCLASS.
WRITE : OBJ->ATTRIBUTE1.
(i.e. object name->attribute name)
METHODS
EVENTS
Static components:
CLASS-DATA
for static attributes
CLASS-METHODS
for static methods
CLASS-EVENTS
for static events
CONSTANTS
for constants (that is, constant static attributes)
TYPES
AND
The declaration part of the class is divided accordingly into up to three sections:
PUBLIC SECTION
PROTECTED SECTION
PRIVATE SECTION
PUBLIC SECTION
All components defined in this section are visible in the subsequent
protected and private sections. These are also externally visible.
PROTECTED SECTION
All components in this section can access the components from the
public section. In turn, they can be accessed by the components of the
private section.
These components are externally visible only to the inherited class.
PRIVATE SECTION
All components of this section can access the components of the public
and private sections. They are not externally visible.
test definition.
public section.
methods: display.
data: stat type i.
private section.
data: newint type I.
Endclass.
Class test implementation.
Method display.
<abap code for method>
Endmethod.
Endclass.
Data: obj type ref to test.
Create object obj.
Call method obj->display.
Obj->stat = 456.
Obj->newint = 123.
METHODS
Introduction to methods
Methods are the functionality of a class , ABAP codes are
written within a method to incorporate the functionality.
Methods are of two types:
1. METHODS meth.Standard Methods.
2. METHODS meth FOR EVENT evt OF class.
(This will be covered later.)This type of methods are
written to trap events.
Let us see an example first of the first variant.
Program :ZEXAMPLE_01
7. ... FINAL
This method cannot be inherited
8.. . .REDEFINITION
( This will be covered during inheritance)
Abstract Class
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 subclasses.
Example:
CLASS order DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS: select_order_header ABSTRACT
EXCEPTIONS no_data_selected
data_found,
select_order_item ABSTRACT.
PROTECTED SECTION.
METHODS: write_hello.
ENDCLASS.
CLASS order IMPLEMENTATION.
METHOD write_hello.
WRITE : /10 'hello,this is a protected method call'.
ENDMETHOD.
ENDCLASS.
Global class
To edit or create a Global Class or interface, the transaction is SE24
For an example let us see the global class Z_EMPLOYEE
Example program : zglobal_class
EVENTS
EVENTS
What is an event ??
Events denote the state of an object, that is
what is the current status of an object, in
laymans word events adds the dimension of time
to the modeled entity. Events enable objects or
classes to trigger event handler methods in
other objects or classes.
1. Creating an event
2. Creating an event handler method for the event
3. Registering the handler method to the event
4. Raising the event
Creating an event
Creating an event
Events can be created like normal class component
CLASS test DEFINITION.
PUBLIC SECTION.
EVENTS: test_event.
ENDCLASS.
Note: events dont have implementation part
Start-of-selection.
Data: my_object type ref to test.
Create object: my_object .
SET HANDLER my_object->event_method FOR test.
(setting the handler, test is the name of the class )
Raising an event
An event can be raised in by using the ABAP statement
raise event. Events can be raised anywhere It may be
raised in any method of a class or may be raised any where in
the main program
CLASS testing_event DEFINITION.
PUBLIC SECTION.
EVENTS my_event.
METHODS event_raising_method.
ENDCLASS.
CLASS testing_event IMPLEMENTATION.
METHOD event_raising_method.
RAISE EVENT my_event.
ENDMETHOD.
ENDCLASS
Example yevent01
Example Code
Example yevent02
Event parameters
Like methods parameters can be passed to events, events can
only have input parameters; any no of parameters can be passed
to an event
Confused??
Lets take an example
CLASS evt_container DEFINITION.
PUBLIC SECTION.
METHODS event_trigger.
EVENTS my_event EXPORTING VALUE(PAR1) TYPE I
VALUE(PAR2) TYPE I OPTIONAL
VALUE(PAR3) TYPE I DEFAULT 3.
ENDCLASS.
CLASS class_evt_handler_method DEFINITION.
PUBLIC SECTION.
METHODS handler_for_event FOR EVENT my_event OF
evt_container IMPORTING SENDER PAR3.
ENDCLASS.
Let us see an example: YEVENT5
Inheritance
Contents
1.Fundamentals of inheritance
2. Member access
3. Method redefinition
4. Concept of Constructor
5. Concept of Final class
6. Concept of Abstract class
7. Polymorphism
8. Summary
Fundamentals of inheritance
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.
In OOP inheritance can be multiple as well as multi-level,
but in ABAP only multi-level inheritance is possible .
FATHER
MOTHER
GRAND FATHER
FATHER
CHILD
CHILD
Member access.
The public and protected components of the super
class are visible in the subclass.
Sub Class
Public Section: Super Class
Protected Section: Super
Class
NOTE THE
PRIVATE
SECTION IS NOT
VISIBLE IN THE
SUB CLASS.
Method Redefinition.
A method can be re-implemented in the
base class using redefinition of the
method.
The parameters remain same in the base
class as it was in super class.
Concept of Constructor.
Case 3
Super class doesnt have explicit constructor
and
Sub class has explicit constructor.
Explicit call required for the constructor of the super class
Case 4
Super class has explicit constructor
and
Sub class has explicit constructor.
Explicit call required for the constructor of the super class
Polymorphism
Polymorphism means dynamic bindings of objects.
There will be one object referring to base class and
dynamically it will take the reference of different
sub class objects
Summary
Few points to be remembered:
THINGS TO BE COVERED
1. Introduction to interface
2. Class and interface
3. Interface reference and component access.
4. Reference variable
5. Concept of Polymorphism
6. Polymorphism via interface.
7. Polymorphism via inheritance.
Introduction to interface
Interface is similar to abstract class but it has many strong
features. Interface unlike class 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
INTERFACE my_interface .
METHODS my_interface_method exporting num type i.
ENDINTERFACE.
CLASS interface_class DEFINITION.
INTERFACES my_interface.
ENDCLASS.
CLASS interface_class IMPLEMENTATION.
METHOD my_interface~my_interface_method.
Write:/ num.
ENDMETHOD.
ENDCLASS.
note
1. The class must implement the methods of all interfaces
implemented in it. The implementation part of the class must
contain a method implementation for each interface method
<imeth>:
METHOD <intf~imeth>.
...
ENDMETHOD.
2.Interfaces can be implemented by different classes. Each of
these classes is extended by the same set of components.
However, the methods of the interface can be implemented
differently in each class.
Reference variable.
a reference variable has two characteristics
1. static
2. dynamic
The static characteristic is the class or interface used in
reference variable definition.
The dynamic type is the object to which the reference
variable is currently pointing.
DATA: ref1 type ref to father, (static)
ref2 type ref to child. (static)
Ref1 = ref2. (dynamic)
Concept of Polymorphism
Polymorphism refers to the property by which objects
belonging to different classes are able to respond to
the same message, but in different forms
Note
an essential requirement of polymorphism is the ability to
refer to objects without any regard to their class. This
necessitates the use of a single reference variable to refer
to the object of different class
1. Via interface
2. Via inheritance.
INTERFACE my_infc.
METHODS: I_method.
ENDINTERFACE.
CLASS c1 DEFINITION.
PUBLIC SECTION.
INTERFACES: my_infc.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD my_infc~I_method.
Write:/ method for c1.
ENDMETHOD.
ENDCLASS.
Example yinterface01
Note:we
START-OF-SELECTION.
DATA: f_ref TYPE REF TO father,
Ref TYPE REF TO father,
C_ref TYPE REF TO child.
CREATE OBJECT: C_ref,f_ref.
Ref = f_ref.
Call method ref->f_meth.
Ref = C_ref.
Call method ref->f_meth.
** call method ref->c_meth . this assignment is not possible.
Example yinterface02