Você está na página 1de 76

Procedural language drawback

What the Gurus say.


1. Program maintenance
2. Difficulty in program management as program size increases
3. Data hiding
4. Very difficult to model real world entity
5. Lack of extensibility

Comparison between oop and procedure oriented


approach
Procedure oriented approach
1. Emphasis on tasks
2. Large programs are divided into smaller programs known as functions
3. Most of the functions share global data
4. Data move openly around the system from function to function
Object oriented approach
1.
2.
3.
4.
5.
6.

Emphasis on things that does those tasks


Programs are divided into objects
Data structures are designed such that they characterized the objects
Functions that operate on the data of an object are tied together in the
data structure
Data can be hidden and cannot be accessed by external functions
New data and functions can be easily added whenever necessary

Object Oriented Approach (key features)

1. Better Programming Structure


2. Real world entity can be modeled very well
3.Stress on data security and access
4. Data encapsulation and abstraction
5. Reduction in code redundancy

Lets be more specific -> OOP and ABAP


Why do we need OOP for ABAP ???
1 To be at par with the others as this is better methodology
2. To understand the recent concept of ABAP ,such as
BAPI BADI WORKFLOW etc.
3. For integration of ABAP with Microsoft Technologies and
JAVA as these are build on OOP concept
4. To exploit the class resources that has been provided by
SAP

What is OOP all about ???

1. Class and objects


2. Constructor
3. Inheritance
4. Events
5. Interfaces and polymorphism

Lets take an example

What are the


attributes of
the box?

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

Introduction to Class and Objects

The main objective of OOP is to model real


world entity

Each real world entity can be modeled by


its characteristics and functionality

The real world entity when modeled into


OOP world is known as Class, characteristics as
attributes and functionality as methods.

Classes can be of two types:


1.Global Class
2.Local Class
Global Classes can be accessed in any program.
Local Classes are limited to the program in which they are
defined.

Global vs. Local Classes

Global Classes

Local Classes

Can be accessed
from

Any program

Only the program


where it is defined

Where Stored

In the Class
Repository

In the program
where it is defined

Where Created

With the Class


Builder (SE24)

With the ABAP


Editor

Namespace

Must begin with Y


or Z

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.

This declares and defines a local class


test .
In ABAP program this belongs to Global
Section.
Class definition cannot be nested.
Classes cannot be defined inside
subroutines or function modules.

But what is an object?????


Objects are instance of a Class or in other
words they are run time entities. We can create
any number of objects for a class.

Creating and accessing Objects.


Data: obj type ref to test.
Create object obj.

WRITE : OBJ->ATTRIBUTE1.
(i.e. object name->attribute name)

The components of class


Instance components:
DATA

for instance attributes

METHODS
EVENTS

for instance methods


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

for internal types in classes

AND

All components must belong to a visibility section.


section Components
can be public, protected or private.

The declaration part of the class is divided accordingly into up to three sections:
PUBLIC SECTION
PROTECTED SECTION
PRIVATE SECTION

**There is no default visibility section in a class.**


**This sequence of visibility must be maintained in a class**

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.

Let us see the exact syntax.


Class

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.

THIS WILL GIVE AN ERROR

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

For the first Variant, the following additions are


there
METHODS meth.
1. ... IMPORTING
Method that has import parameters
2. ... EXPORTING
Method that has export parameters
3 RETURNING VALUE (P)
Method that returns values.
4 CHANGING
Method that changes values that is passed.
5. ... EXCEPTIONS p1 ...
Methods that raise exceptions.covered later)
6. ... ABSTRACT
(This will be covered with Abstract classes.)

7. ... FINAL
This method cannot be inherited
8.. . .REDEFINITION
( This will be covered during inheritance)

Let us see the example ZEXAMPLE_08


METHODS can raise exceptions. Lets see how.
Zexample07.

9. And a special method called constructor


Each class has one constructor. It is a predefined, public
instance method of the class, with the name CONSTRUCTOR.
Constructors are special methods that produce a defined initial state
of objects and classes. The state of an object is determined by its
instance attributes and static attributes. You can assign contents to
attributes using the VALUE addition in the DATA statement.
Constructors are necessary when you want to set the initial state of an
object dynamically.

Constructors are executed once for each instance. They


are called automatically directly after you have created an
instance with the CREATE OBJECT statement.

A constructor like a method can raise exceptions.


For Example:
Create object obj exporting param = number
Exceptions exception1.
All input parameters ( IMPORTING, CHANGING parameters) can be defined during
declaration as optional parameters using the additions OPTIONAL or DEFAULT.
These parameters must not necessarily be transferred when the method is called.

Another Example : Program Zexample_04

Methods can import as well as export Internal Tables.


Let us consider another small example
zexample05

What are static Components?


Static components of a class may be defined to be
the ones, that are property of a class.
Unlike instance attributes, which exist separately
for each instance (object) of a class, and can only
be used after one has created at least one
instance, static attributes exist once only and are
shared by all instances in the class.
Unlike instance methods, which can access both
instance and static attributes, and trigger both
instance and static events, static methods can only
access static attributes.
Example : example02.

Coming Back to Class.


Some more features of CLASS.
CLASS class DEFINITION DEFERRED.
This is used in forward referencing.
Example:
CLASS C2 DEFINITION DEFERRED.
CLASS C1 DEFINITION.
PUBLIC SECTION.
DATA O2 TYPE REF TO C2.
ENDCLASS.
CLASS C2 DEFINITION.
PUBLIC SECTION.
DATA O1 TYPE REF TO C1.
ENDCLASS.

CLASS class DEFINITION LOAD.

If the first access to a global class in a program is to its


static components then explicit loading of the class
definition is necessary.
Class classxyz definition create public| protected | private.
CREATE PUBLIC addition is provided automatically by
compiler if no create addition is used.
The additions CREATE PROTECTED and CREATE PRIVATE
allow you to control the instantiation of your class.
RPOGRAM: EXAMPLE03

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.

Abstract class serves as a template for subclasses


In a abstract class at least one method must be abstact or
if a class contains any abstract method the whole class
becomes abstract.

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.

Four stages of an event life

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

Event handler method


Methods are only executed when the event associated
with these methods are triggered.
How to write event handler method?
CLASS test DEFINITION.
PUBLIC SECTION.
METHODS: event_method FOR EVENT
event_name OF CLASS my_class.
ENDCLASS.

Few points to note


1.an event can have multiple event handler method
associate with it
2. The sequence in which the registered handlers are
executed is the same as the sequence in which they are
registered.
3.Handler methods cannot be called using call function.
4.After the RAISE EVENT statement, all of the registered
handler methods are executed before the next statement is
processed

Setting the handler for the event (registering the


event)
To automatically execute the event handler method when the event
is raised, the handler method has to be registered.
The registration process is dynamic i.e the link is established at
runtime. The key statement set handler can register the handler
method.

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

Dynamic registration and de-registration of events

Handler methods can be registered or de-registered dynamically


by the optional key word ACTIVATION . Activation can register
new method or de-register existing method using a variable .The
variable must be character type of length 1.

1.If ACTIVATION is X the method registered


2. If ACTIVATION is the method de registered

Example Code

Data: my_object type ref to test. test is class


Create object: my_object.
SET HANDLER my_object->event_method FOR test.
registering the handler
SET HANDLER my_object->event_method FOR test
ACTIVATION .
de-registering the handler
SET HANDLER my_object->event_method FOR test
ACTIVATION X.
registering the handler.

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

what does the event do with the input parameters?

The parameters that are passed to the events are utilized by


the handler methods. The handler methods have the liberty to
decide on the number of input parameters passed by the
events.

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

System variable check(sy-subrc) for event handling.

1. If sy-subrc = 0 then all event handler methods have


been properly registered
2. SY-SUBRC = 4: then you tried to register the
same combination of triggering event, event handling
method, and handler more than once.
3. If sy-subrc = 8: then you tried to deregister an
event handler that was not registered.

Non-cacheable runtime error during event handling.

SET_HANDLER_DISP_OVERFLOW: Unable to register any more


handlers.
SET_HANDLER_E_NO_FOR: Handlers of instance methods need
the FOR addition.
SET_HANDLER_FOR_CE: Event handler registered for a static
event.
SET_HANDLER_FOR_NULL: An event may not be triggered using
NULL.

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.

Private components of the sub class can have same


name as the private component of the super class.
Public and Protected components can not have the
same name as that have been used in super class.
Super Class
Public Section: Super Class
Private Section: Super Class
Protected Section :Super Class

Sub Class
Public Section: Super Class
Protected Section: Super
Class

NOTE THE
PRIVATE
SECTION IS NOT
VISIBLE IN THE
SUB CLASS.

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
PROTECTED SECTION.
Protected components
Protected components
PRIVATE SECTION.
Private section
ENDCLASS.

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.

Example of method redefinition.


CLASS gui_status DEFINITION.
PUBLIC SECTION.
. . . . . .
METHODS set_gui_status.
. . . . . . .
ENDCLASS.
CLASS sub_class_one DEFINITION INHERITING FROM gui_status.
PUBLIC SECTION.
. . . . .
METHODS set_gui_status REDIFINITION.
. . . . . . .
ENDCLASS.
CLASS sub_class_twoFINITION INHERITING FROM gui_status.
PUBLIC SECTION.
. . . . .
METHODS set_gui_status REDIFINITION.
. . . . . . .
ENDCLASS.
CLASS super_class IMPLEMENTATION.
METHOD set_gui_status method.
SET pf-status ZSUPER
ENDMETHOD.
ENDCLASS.
CLASS sub_class_one IMPLEMENTATION.
METHOD set_gui_status method.
SET pf-status ZSUB1
ENDMETHOD.
ENDCLASS.
CLASS sub_class_two IMPLEMENTATION.
METHOD set_gui_status method.
SET pf-status ZSUB2
ENDMETHOD.
ENDCLASS.

Concept of Constructor.

What happens if we want to instantiate an object of sub class when:


Case 1
Super class doesnt have explicit constructor
and
Sub class doesnt have explicit constructor.
No explicit call required for the constructor of the super class
Case 2
Super class has explicit constructor
and
Sub class doesnt have explicit constructor.
No explicit call required for the constructor of the super class

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

Concept of Final class


A final class can not be inherited further.
CLASS final_class DEFINITION FINAL. this class cannot be inherited
. . . . . . . .
ENDCLASS.
CLASS derived_class DEFINITION inheriting from final_class .
. .. . . . . . . .
ENDCLASS.
A final method can not be redefined further.
CLASS super_class DEFINITION .
. . . . . . . .
methods final_method final.
ENDCLASS.
CLASS sub_class DEFINITION inheriting from super_class .
. .. . . . . . . .
methods final_method redefinition.
ENDCLASS.

Concept of Abstract class


An abstract class can be inherited in the same way
like a normal class.
A method, which is Abstract, should be redefined
in derived class.
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.

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

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. >> calls the sub_class_one
supr_obj = sub2_obj.
CALL METHOD supr_obj->test_method. >> calls the sub_class_two

Summary
Few points to be remembered:

Only Multi-level inheritance.


An abstract class can be inherited in the same way like a normal
class but every abstract method should be redefined
Final class can not be inherited
Public and Protected sections are visible to the sub class
A method can be redefined in the sub class but not the Final
method.
Super class constructor calling depends upon explicit definition
of the Sub class constructor.

INTERFACE POLYMORPHISM AND


INHERITANCE

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

What is the benefit??


This allows users to address different classes via a universal
point of contact.

How to write simple interface


INTERFACE my_interface.
Data : name(20).
METHODS: I_method1,
I_method2.
ENDINTERFACE.

Class and interface.


Class and interface are interrelated, an interface cannot
be implemented without a class . Interface do not have
instances. Interface can be implemented in a class using
the statement INTERFACES <interface_name> in the
public section of a class. The component of the
interface are added automatically in the public section
of the class
Now the question comes how do we access the components of
the interface ??

Well the components of an interface can be


identified as if they are components of the
class, through the identifier
<interface_name~interface _component >

Lets take an example.

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.

Lets take an example


INTERFACE common_interface.
METHODS: interface_method.
ENDINTERFACE.
CLASS interface_class1 DEFINITION.
PUBLIC SECTION.
INTERFACES common_interface.
ENDCLASS.
CLASS interface_class1 IMPLEMENTATION.
METHOD common_interface~interface_method. interface method is implemented as type
1
WRITE:/ METHODS IMPLEMENTATION TYPE 1
ENDMETHOD.
ENDCLASS.
CLASS interface_class2 DEFINITION.
PUBLIC SECTION.
INTERFACES common_interface.
ENDCLASS.
CLASS interface_class2 IMPLEMENTATION.
METHOD common_interface~interface_method. Interface method is implemented astype2
WRITE:/ METHODS IMPLEMENTATION TYPE 2.
ENDMETHOD.

Interface reference and component access


Using the class reference variable <cref>:
To access an attribute <attr>: <cref>-><intf~attr>
To call a method <meth>: CALL METHOD <cref>-><intf~meth

Using the interface reference variable <iref>:


To access an attribute <attr>: < iref>-><attr>
To call a method <meth>: CALL METHOD <iref>-><meth>

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)

Reference variable assigning rule.

When the static and the dynamic type of


a reference variable are different, the
principal rule is that the static type is
always more general than the dynamic
type. For example if the static type is an
interface the dynamic type can be a class
implementing the interface.

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

how do we achieve polymorphism ???

1. Via interface
2. Via inheritance.

Polymorphism via interface


Interfaces allow you to use different classes in a uniform
way using interface references
As we know that any no of class can implement the
interface differently ,and we have seen that how we can
access the components of the interface through the class
objects

But we want to have a common point of access for


the different implementation of the interface
methods??
we can do that by using an interface reference; we can
access the different class objects by pointing the interface
reference to that class.

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

Polymorphism via inheritance.


With inheritance, a reference variable defined with respect to a
class c1 may not only point to instances of c1 but also to
instances of subclasses of c1.
1. You can even create subclass objects using a reference variable
typed with respect to a superclass
2.Using inheritance only, the static type is either the same as
the dynamic type or is a superclass of the dynamic type
3. In other words, we can use the reference of super class to
point to the instance of the sub class, and using this reference
we can access the components of the subclass.

Note:we

can access only those components of the


subclass that it has inherited from the super class.

CLASS father DEFINITION.


PUBLIC SECTION.
METHODS: f_meth.
ENDCLASS.
CLASS father IMPLEMENTATION.
METHOD f_meth.
Write: / father method.
ENDMETHOD.
ENDCLASS.
CLASS child DEFINITION INHERITING FROM FATHER.
PUBLIC SECTION.
METHODS : f_meth REDEFINITION,
C_meth.
ENDCLASS.
CLASS child IMPLEMENTATION.
METHOD f_meth.
Write: father method redefined.
ENDMETHOD.
METHOD c_meth.
Write: child method.
ENDMETHOD.
ENDCLASS.

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.

*************** out put ***********************


father method
father method redefined.

Example yinterface02

Você também pode gostar