You are on page 1of 42

07/09/15

Unstructured Programming.
Procedural Programming.
Object Oriented Programming.

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

report ysubdel.
DATA : sal type p decimals 2,
itax type p decimals 2,
net_sal type p decimals 2 .
sal = 12000.
IF sal lt 5000 .
itax = 0.
ELSE.
itax = sal * '0.01'.
ENDIF.
net_sal = sal - itax.
write:/5 sal , itax ,
net_sal.
sal = 3500.
IF sal lt 5000 .
itax = 0.
ELSE.
itax = sal * '0.01'.
ENDIF.
net_sal = sal - itax.
write:/5 sal , itax ,
net_sal.
2005Intelligroup,Inc.
07/09/15

proprietary

Characteristics
Consists of only one main program.
Data access done always globally

Disadvantages
Difficult to manage once the program
becomes large.
Same sequence of statements are
repeated at multiple places.
Confidential and

report ysubdel.
DATA : sal type p decimals 2 ,
itax type p decimals 2 ,
net_sal type p decimals 2.
sal = 12000.
PERFORM sub_calc_tax USING
sal itax net_sal.
sal = 3500.
PERFORM sub_calc_tax USING
sal itax
net_sal.

A procedure call is used to invoke the


procedure.
After the sequence is processed, flow
of control proceeds right after the
position where the call was made.

FORM sub_calc_tax USING


P_SAL P_ITAX P_NET_SAL.
IF p_sal lt 5000 .
p_itax = 0.
ELSE.
p_itax = sal * '0.01'.
ENDIF.
p_net_sal = p_sal - p_itax.

write:/5 sal , itax , net_sal.


2005Intelligroup,Inc.

07/09/15

proprietary

Confidential and

What's an Object and Class?


Class
A class is a set of objects that share a common
structure and a common behavior.
Object
An object has state, behavior, and identity; the
structure and behavior of similar objects are
defined
in their common class; the terms instance and
object
are interchangeable.
2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Some Classes & Their Objects


Maruthi 800

Rajas Maruthi,
Prasads Maruthi,
Ramanis Maruthi

Customer

Pepsi,bike,samsung,tv

SalesOrder

OR2643789, OR2643799
OR2643776, OR9999999

Cricket Team

IndianTeam, Australian
Team, SrilankanTeam

Your Desktop PC

IGA51097 etc.

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Sample Attributes & Methods


Class

Attributes

Methods

CricketTea
m

Captain,
VC, WC,
FB1,FB2,FB3
SP1,SP2,SUB

DoSingle,DoDouble
DoBowl,DoCatch
DoRunout,HitSix,
Doplay,HitBowndary

ICICI S.A.

Account
Number,
Balance,
CreditLimit
CheckBooks

ATM_Transfer,
E_transfer, Withdraw,
Check_Credit_limit,
Issue_check_book,
Track_transactions

Production

PO#,SSD,SE Start_Production,End
D,ASD,AED,C _production,Start_opr
omp,CoOptn ,Send_to_WS Etc
,Oper,Workc
enter

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Basic Object-oriented
Principles
Abstraction
Encapsulation

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Abstraction
Abstraction is used to manage complexity

Abstraction is the process of hiding the


details and exposing only the essential
features of a particular object.
Abstraction is used specifically in the Object
Oriented Programming (OOP) sense
hiding the details of the implementation of
an object behind the interface composed of
its methods.
2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Encapsulation
Wrapping of data and methods into a single unit
(called class) is known as encapsulation.
The data is not acceseble to the outside world
only those methods ,Which are wrapped in the
class can only access it by using following
visibility sections.
Public,
Protected and
Private,
are the basis for one of the important features of
object orientation - encapsulation.
2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

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

Classes
Class describes an object
components of the class describe the state

and behavior of objects.

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Classes
Global Class

Local Class

-> Defined in transaction


se24
->Accessed by any
program

-> Defined in trasaction


se38
->Accessed by only in the
program where it is
defined.
->Begin with any character
->

->Must begin with y or z


->Stored in Class
Repository

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and


A Class basically contains the following:-

Attributes:- Any data,constant ,types declared within a class form the


attribute of the class.

Methods:- Block of code, providing some functionality offered by the class.


Can be compared to function modules.

Events:- A mechanism set within a class which can help a class to trigger
methods of other class.

Interfaces:-Interfaces are independent structures that you can implement in


a class to extend the scope of that class.
2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Classes
Defining Local Classes:
A complete class definition consists of a

declaration part and, if required, an


implementation part.
The declaration part of a class <class>
CLASS <class> DEFINITION.
...
ENDCLASS.
It contains the declaration for all components
(attributes, methods, events) of the class.
The declaration part belongs to the global
program data.
2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Classes
If you declare methods in the declaration part of

a class, you must also write an implementation


part for it. This consists of a further statement
block:

CLASS <class> IMPLEMENTATION.


...
ENDCLASS
The implementation part of a local class is a
processing block. Subsequent coding that is not
itself part of a processing block is therefore not
accessible.
2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

REPORT

YSUBOOPS17

CLASS c1 DEFINITION.
PUBLIC SECTION.
data : w_num type i value 5.
methods : m1.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD M1.
WRITE:/5 'I am M1 in C1'.

Defined in the global area of a


local program :CLASS <class name> DEFINITION.
..
ENDCLASS.

All the attributes , methods,


events and interfaces are
declared here.
Cannot be declared inside a
subroutine/function module.

ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA : oref1 TYPE REF TO c1 .

Class definition cannot be


nested.

CREATE OBJECT : oref1.


write:/5 oref1->w_num.
CALL METHOD : oref1->m1 .

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

REPORT

YSUBOOPS17

CLASS c1 DEFINITION.
PUBLIC SECTION.
data : w_num type i value
5.
methods : m1.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD M1.

Local class in a program is implemented as


follows:CLASS <class name>
IMPLEMENTATION.
..
ENDCLASS.
Methods used by the class are described here.
A class can be implemented

WRITE:/5 'I am M1 in C1'.


ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.

At the end of the program( like


subroutines).
After the class definition.

DATA : oref1 TYPE REF TO


c1 .
CREATE OBJECT : oref1.
write:/5 oref1->w_num.
CALL METHOD : oref1->m1 .
2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Class implemented at the end of


the program

2005Intelligroup,Inc.
07/09/15

proprietary

Class implemented after Definition

Confidential and

Constructors:
Special Methods:
CONSTRUCTOR:
Cannot call with CALL METHOD statement.
Called automatically when you create an object
CLASS_CONSTRUCTOR:
Called when you first access the components of a class
Events:
Objects or classes can use events to trigger event

handler methods in other objects or classes.


When an event is triggered, any number of event
handler methods can be called.
the handler determines the events to which it wants to
react. There does not have to be a handler method
registered for every event.
2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Classes : Visibility Sections


You can divide the declaration part of a class into up to three

visibility areas:
CLASS <class> DEFINITION.
PUBLIC SECTION.
...
PROTECTED SECTION.
...
PRIVATE SECTION.
...
ENDCLASS.
They define the external interface of the class to its users
Encapsulation :
The public components of global classes may not be changed
once you have released the class.
As well as defining the visibility of an attribute, you can also
protect it from changes using the READ-ONLY addition.
2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

There is no default visibility section in a


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

CLASS C1

DEFINITION.
PUBLIC
SECTION.
DATA:
METHODS:
EVENTS:
PROTECTED
SECTION.
DATA:
METHODS:
EVENTS:
PRIVATE
SECTION.
DATA:
METHODS:
EVENTS:
ENDCLASS.
2005Intelligroup,Inc.
07/09/15

proprietary

A Class puts its components

under three distinct sections:Public Section:- Components


placed here 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 Section:- Components
placed in protected section are
visible to the children of the
class(subclass) as well as within
the class
Private Section:-Components
placed here are accessible by the
class itself.
Confidential and

Subclass
of the
class

Class
itself

REPORT YSUBOOPS17 .
CLASS c1 DEFINITION.
PUBLIC SECTION.
data : w_num type i value 5.
methods m1.
ENDCLASS.

class c2 definition inheriting


from c1.
public section .

CREATE OBJECT : oref1.

method m2.

method m1.

write:/5 From subclass' ,


w_num .

write:/5 From class : ' , w_num.

endmethod.

endmethod.

endclass.

2005Intelligroup,Inc.
proprietary

DATA :
oref2 type ref to c2 .

endclass.

ENDCLASS.

07/09/15

START-OF-SELECTION.
oref1 TYPE REF TO c1 ,

methods : m2.

class c2 implementation.

CLASS c1 IMPLEMENTATION.

Externa
l user

Confidential and

write:/5 As an user ' ,


oref1->w_num.
Call method oref1->m1.
Call method oref2->m2.

Self-Reference
Internally, each method has an implicit

self-reference variable, the reserved


word me
A method can access the components of
its class simply by their name; however,
It may use me simply for clarity
If a method declares a local variable
with the same name as one of the
class components, to avoid ambiguity
it must use me to address the variable
originally belonging to the class.
A method must use me to export a
reference to itself (that is, its object)

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Inheritance
Inheritance allows you to derive a new class from an

existing class.
CLASS <subclass> DEFINITION INHERITING FROM
<superclass>.
The new class <subclass> inherits all of the
components of the existing class <superclass>.
However, only the public and protected components of
the super class are visible in the subclass.
You can declare private components in a subclass that
have the same names as private components of the
super class. Each class works with its own private
components.
2005Intelligroup,Inc.

07/09/15

proprietary

Confidential and

Subclasses can be created from its


superclass using the syntax:CLASS <subclass> DEFINITION
INHERITING FROM <superclass>.
Subclass inherits all the public and protected
components of the superclass.
Superclass should not be declared as a
FINAL class.

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Inheritance
You can add new components to the subclass.

This allows you to turn the subclass into a


specialized version of the super class.
A class can have more than one direct
subclass, but it may only have one direct
super class. This is called single inheritance.
The root node of all inheritance trees in ABAP
Objects is the predefined empty class OBJECT.

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Inheritance
Redefining Methods: you can use the REDEFINITION

addition in the METHODS statement to redefine an


inherited public or protected instance method in a
subclass and make its function more specialized.
The implementation of the redefinition in the subclass
obscures the original implementation in the super class.
Any reference that points to an object of the subclass
uses the redefined method, even if the reference was
defined with reference to the superclass.
This particularly applies to the self-reference ME->.
Within a redefined method, you can use the pseudo
reference SUPER-> to access the obscured method.
2005Intelligroup,Inc.

07/09/15

proprietary

Confidential and

Final classes cannot have subclasses. Only the


class can be instantiated.
CLASS <classname> DEFINITION FINAL.

A final method cannot be redefined in


subclasses
METHODS <method_name> .FINAL

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

To redefine a public/protected method of a


superclass in one of its subclasses, use the syntax
in the subclass definition:METHOD <method name> REDEFINITION

The interface and visibility of a method cannot


be changed while redefining it.

The method declaration and implementation in


the superclass is not affected when you redefine
the method in a subclass.

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

One cannot create an object from an abstract


class. Only subclasses can be derived from
them.
CLASS <classname> DEFINITION ABSTRACT.

Abstract methods cannot be implemented in


the same class. Only the subclasses of that
class can implement it.
METHODS <method_name> .ABSTRACT

Any class containing an abstract method has


to be an abstract class. All subsequent
subclasses that do not implement the method
must also be abstract. To implement an abstract
method in a subclass, one need to redefine this
subclass using the REDEFINITION addition.

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

CLASS c1 DEFINITION.
PUBLIC SECTION .
CLASS-DATA : num TYPE I VALUE 5 .
ENDCLASS.

CLASS c1 IMPLEMENTATION.
ENDCLASS.

CLASS c2 DEFINITION INHERITING FROM


c1.
ENDCLASS.

CLASS c2 IMPLEMENTATION.
ENDCLASS.

START-OF-SELECTION.
c2=>num = 7.
write:/5 c1=>num .

Output :

They are visible in all


classes in the inheritance
tree.

7
2005Intelligroup,Inc.

07/09/15

Static attributes only exist


once in each inheritance
tree. One can change them
from outside the class using
the class component selector
with any class name, or
within any class in which they
are shared.

proprietary

Confidential and

class c1 definition.
. . . . . . .
endclass.
class c1 implementation.
. . . . . .
endclass.
class c2 definition inheriting from c1.
. . . . . .
endclass.
class c2 implementation.
. . . . . . .
endclass.
start-of-selection.
data : oref1 type ref to c1,
oref11 type ref to c1,
oref2 type ref to c2.
create object oref1 type c2 .
create object oref2.
oref11 = oref2.
write:/5 oref1->num ,
oref11->num .
2005Intelligroup,Inc.
07/09/15

proprietary

With inheritance, a reference variable


defined with respect to a class may not only
point to instances of that but also to
instances of subclasses of the same. One
can even create subclass objects using a
reference variable typed with respect to a
super class.
Polymorphism through inheritance can be
achieved by playing with static and dynamic
type of a reference variable.
Instances of a subclass may be used
through the super class's interface. When
this is done, a client can't access all
components defined in the subclass, only
those inherited from the respective super
class.

Confidential and

Inheritance
Inheritance Overview:

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Inheritance
Inheritance and Reference Variables

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Interfaces
Interfaces are independent structures that you can

implement in a class to extend the scope of that class.a


universal point of contact.

They provide one of the pillars of polymorphism, since they

allow a single method within an interface to behave


differently in different classes.
Global & Local Interfaces

The definition of a local interface <intf> is enclosed in


the statements:

INTERFACE <intf>.

...

ENDINTERFACE.
The definition contains the declaration for all components
(attributes, methods, events) of the interface.
They automatically belong to the public section of the class
in which the interface is implemented.
2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Triggering and Handling


Events
To trigger an event, a class must
Declare the event in its declaration part

Trigger the event in one of its methods


EVENTS <evt> EXPORTING... VALUE(<ei>)
TYPE type [OPTIONAL]..
To declare static events, use the following
statement:

CLASS-EVENTS <evt>...

Both statements have the same syntax.

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Class-based exceptions are handled using TRYCATCHENDTRY


block.
REPORT YSUBCLASS_EXCEPTION.

TRY.
< code to be checked for
exception>

CATCH cx1 .cxn [ into


ref].
< exception handling code>.

DATA: i TYPE i VALUE 1.

START-OF-SELECTION.
TRY.
i = i / 0.

ENDTRY.

CATCH cx_sy_zerodivide.
write:/5 'Divide by zero caught'.
ENDTRY.

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

5 Reasons OO Programming is
better than
Procedural Programming
Data Encapsulation
Instantiation
Code Reuse
Interfaces
Events

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

ABAP Objects is more Explicit and


Simpler to Use

ABAP Objects is much simpler and less error-

prone
because :
Classes contains attributes and methods.
Objects are instances of Classes.
Objects are addressed via references.
Objects have clearly defined interfaces.

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Comparison between Procedural ABAP


and Object oriented ABAP

Procedural ABAP

Object Oriented ABAP

Contains Obsolete

Prohibits obsolete

Statements.
Supports Overlapping and
and some specialized
objects.
Shows Implicit Behavior.
Appears difficult to learn.

statements and additions.


Requires implicit syntax
completions to be explicit.
Detecting and preventing
incorrect data handling.

2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

Improve your Procedural


Programming
using ABAP Objects
Use methods as much as possible.
Replace the use of Subroutines using static

methods.
Use function modules only when technically
necessary.
Disentangle procedural ABAP from ABAP Objects.
Decouple screen programming from application
programming.
Never uncheck the Unicode checks active
checkbox.
2005Intelligroup,Inc.
07/09/15

proprietary

Confidential and

You might also like