Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

OO ABAP

Clonut Solutions Pvt Ltd


Topics to cover
● What is OOPS?
● Different approaches to Programming
● Class and objects
● Methods
● Constructor
● Inheritance
● Polymorphism
● Interfaces
● Encapsulation
● Events Clonut Solutions Pvt Ltd
What is OOPS?
Object Oriented Programming is a methodology or paradigm to design
a program using classes and objects. It simplifies the software
development and maintenance by providing some concepts:
● Class
● Object
● Inheritance
● Polymorphism
● Abstraction
● Encapsulation

Clonut Solutions Pvt Ltd


Different approaches of Programming

● Unstructured Programming.
● Procedural Programming.
● Object Oriented Programming.

Clonut Solutions Pvt Ltd


Comparison between Procedural and Object Oriented
Programming
Procedure Oriented approach Object Oriented approach
Features
Emphasis Emphasis on tasks Emphasis on things that does those
tasks
Modularization Programs are divided into smaller Programs are organized into classes
programs known as functions and objects and the functionalities are
embedded into methods of a class.
Data security Most of the functions share global Data can be hidden and cannot be
data accessed by external sources.

Extensibility Relatively more time consuming to New data and functions can be easily
modify for extending existing added whenever necessary
functionality.

Clonut Solutions Pvt Ltd


Class and Object
Classes and Objects are basic concepts of Object Oriented Programming
which revolve around the real life entities.
Class:
A class is a user defined blueprint or prototype from which objects are
created. It is a collection of objects like -
methods,attributes,events,interfaces etc.
Example : A blueprint of house .

Clonut Solutions Pvt Ltd


Object:
An object is a special kind of variable that has distinct characteristics and
behaviors. It is a pattern or instance of a class.

An object consists of :

● State: It is represented by attributes of an object. It also reflects the


properties of an object.
● Behavior: It is represented by methods of an object. It also reflects the
response of an object with other objects.
● Identity: It gives a unique name to an object and enables one object to
interact with other objects.

Clonut Solutions Pvt Ltd


Local and Global Classes
Global Class Local Class

Accessed by Any program Only the program where it is


defined.
In the Class Repository Only in the program where it is
Stored in defined.
Created using transaction SE24 Created using SE38
Created by
Must begin with Y or Z Can begin with any character
Namespace

Clonut Solutions Pvt Ltd


Example:
Class lcl_A DEFINITION. "class declaration
PUBLIC SECTION. "access modifier
DATA name TYPE char10."attributes
METHODS m1 . "behaviours
ENDCLASS.
DATA lv_obj TYPE REF TO lcl_A. "declaring object of class
"there is two way to create object of class after release 7.4
CREATE OBJECT lv_obj. "or
DATA(lv_obj) = NEW lcl_A( )."incline declaration with New keyword "supported only release after 7.4 ve
CLASS lcl_A IMPLEMENTATION.
METHOD m1.
ENDMETHOD.
ENDCLASS. Clonut Solutions Pvt Ltd
Instance and Static Components
REPORT YATOOPS17 . • Instance components exist separately in
CLASS c1 DEFINITION.
PUBLIC SECTION.
each instance (object) of the class and
data : i_num type i value 5. are referred using instance component
class-data : selector using ‘🡪’.
s_num type i value 6 . • Static components only exist once per
ENDCLASS.
class and are valid for all instances of
CLASS c1 IMPLEMENTATION. the class. They are declared with the
ENDCLASS. CLASS- keywords
START-OF-SELECTION.
• Static components can be used without
DATA : oref1 TYPE REF TO c1 . even creating an instance of the class
CREATE OBJECT : oref1. and are referred to using static
write:/5 oref1->i_num. component selector ‘ =>’ .
write:/5 c1=>s_num .
write:/5 oref1->s_num.
Clonut Solutions Pvt Ltd
Methods
● Methods are the internal procedures of a
class .
● Have an interface.
I E
● One can draw the analogy between methods
C Method C and function modules in ABAP.

Instance methods:
X ● Can use both static and instance components
in the implementation part
I R ● Can be called using the instance name.
Method
Static methods:
X ● Can only use static components in the
I – Import Parameters E-Export implementation part
Parameters ● Can be called using the class name.
C – Changing Parameters R- Returning
Parameters
Clonut Solutions Pvt Ltd
X - Exceptions
Defining and calling methods Example

Clonut Solutions Pvt Ltd


Methods can raise exceptions

● Declare the name of the exceptions while


defining the method in the class definition.

● Raise the exceptions within the method using


any of the following syntaxes:-
RAISE <exception name>
MESSAGE …RAISING <exception>

● Handle the exceptions while calling method


with different values of sy-subrc.

Clonut Solutions Pvt Ltd


Constructor
● Nothing but a special method with parameters.

● Name : “CONSTRUCTOR’’ or “CLASS_CONSTRUCTOR”

● There are restrictions in the construction method parameters.

● Not able to call explicitly.

Clonut Solutions Pvt Ltd


Instance Constructor
● Has one instance constructor for a class.
● Whenever the objects are getting created - more than one call.
● Cannot be in the more specific than the visibility of the instance (PUBLIC).
● Importing parameter and exceptions are allowed, no exporting parameter.

Static Constructor
● Single constructor for a class.
● Before the class is accessed for the first time.
● Must be in the public visibility area.
Clonut Solutions Pvt Ltd
● No parameters and exceptions are allowed.
Constructor Example

Output

Clonut Solutions Pvt Ltd


Inheritance
Inheritance is an important pillar of OOP(Object-Oriented Programming). It is
the mechanism in ABAP by which one class is allowed to inherit the
features(fields and methods) of another class.
● A single class can give birth to its subclasses by inheritance.
● One parent class can have multiple subclasses, but one subclass can have
only one parent class.
● Private components of parent classes are not known to subclasses.
● Multiple inheritance is not possible through classes, but can achieve with
the help of interfaces.

Clonut Solutions Pvt Ltd


Creating subclass Modifying methods in subclass

Clonut Solutions Pvt Ltd


Abstract Methods and Classes
● 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.
Clonut Solutions Pvt Ltd
Final Methods and Classes

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

Clonut Solutions Pvt Ltd


Inheritance and Static Attributes
CLASS C1 DEFINITION.
PUBLIC SECTION .
CLASS-DATA : num TYPE I VALUE 5 .
ENDCLASS. ● Static attributes only exist once in each
inheritance tree. One can change them
CLASS c1 IMPLEMENTATION. from outside the class using the class
ENDCLASS. component selector with any class name,
or within any class in which they are
CLASS C2 DEFINITION INHERITING FROM C1. shared.
ENDCLASS. ● They are visible in all classes in the
inheritance tree.
CLASS C2 IMPLEMENTATION.
ENDCLASS.

START-OF-SELECTION.
c2=>num = 7.
write:/5 c1=>num . Clonut Solutions Pvt Ltd
Inheritance -> Instance Constructors

Clonut Solutions Pvt Ltd


Polymorphism
class c1 definition.
.......
endclass.
class c1 implementation.
...... ● Polymorphism means multiple forms.
endclass. ● It is characteristic of being able to assign a
class c2 definition inheriting from c1. different behaviour or value in a subclass, to
...... something that was declared in a parent
endclass.
class.
class c2 implementation.
.......
● For example - a method can be declared in a
endclass. parent class, but each subclass can have a
start-of-selection. different implementation of that method.
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 , Clonut Solutions Pvt Ltd
oref11->num .
Method overloading
Method overloading occurs within the class.
In method overloading, methods must have the same name and different
signatures.
–Method overloading is not possible in ABAP OOPS.--compile time
polymorphism
Method overriding
It is performed in two classes with inheritance relationship ( superclass
method and subclass method).
In method overriding, methods must have the same name and same
signature.--runtime polymorphism
Clonut Solutions Pvt Ltd
Narrow/ up Casting
When we assign the instance of the Subclass
back to the instance of the Superclass, than it is
called the “Narrowcasting”, because we are
switching from a “More Specific view of an
object” to “less specific view”.

When we assign the instance of the Superclass


to the Subclass, than it is called the Widening
Cast, because we are moving to the “More
Specific View” from the “Less specific view”.

In ABAP, it is necessary to catch the exception


CX_SY_MOVE_CAST_ERROR while doing
the widening cast to avoid the short-dump.
Clonut Solutions Pvt Ltd
Interface
● Independent structures
containing definition of its
own components.

● Incorporated in the public


section of a class definition
and all the methods of the
interface are abstract
methods - there is only
definition, no
implementation.

Clonut Solutions Pvt Ltd


Multiple Inheritance using interface

Clonut Solutions Pvt Ltd


Encapsulation
Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism
that binds together code and the data it manipulates.

● Technically in encapsulation, the variables or data of a class


is hidden from any other class and can be accessed only
through any member function of its own class in which it is
declared.
● combination of data-hiding and abstraction.
● Encapsulation can be achieved by Declaring all the variables
in the class as private and writing public methods in the class
to set and get the values of variables

Clonut Solutions Pvt Ltd


Encapsulation example
REPORT ZATY_OO_EX4. START-OF-SELECTION.
PARAMETERS: p_kunnr TYPE kunnr, DATA: obj1 TYPE REF TO lcl_customers,
p_land1 TYPE land1_gp. wa_customer type kna1.
CLASS lcl_customers DEFINITION.
PUBLIC SECTION.
METHODS: set_customer IMPORTING im_kunnr TYPE kunnr create OBJECT obj1.
im_name1 TYPE name1_gp obj1->set_customer( EXPORTING im_kunnr = p_ku
im_land1 TYPE land1_gp, nnr im_name1 = p_name1 im_land1 = p_land1 ).
get_customer EXPORTING wa_cus TYPE kna1.
PRIVATE SECTION. obj1->get_customer( IMPORTING wa_cus = wa_custo
CLASS-DATA: wa_kna1 TYPE kna1, mer ).
wa_cus TYPE kna1. write:/ 'no', 20 'name' CENTERED, 46 'country'
ENDCLASS. .
CLASS lcl_customers IMPLEMENTATION.
METHOD set_customer. WRITE:/ wa_customer-kunnr COLOR 1,
wa_kna1-kunnr = im_kunnr. wa_customer-name1 COLOR 3,
wa_kna1-name1 = im_name1. wa_customer-land1 COLOR 1.
wa_kna1-land1 = im_land1.
ENDMETHOD.
METHOD get_customer.
wa_cus = wa_kna1.
ENDMETHOD.
ENDCLASS.
Clonut Solutions Pvt Ltd
Events
Raising events in Object Oriented Programming is a mechanism by which
methods of one class can call methods of same/other classes.
List of To-do’s
1.Declare an event in a class.
Class 1 Class 2 2.Declare a triggering method
and implement it in the same
Eve Event Handler class.
nt Method 3.Declare an event handler
method in same/ other class.
4.Register event handler
Event
method
Triggering
With the event at run time.
Method
5. Call the triggering method.

Clonut Solutions Pvt Ltd


Events - Implementation
● Events
A class can contain events as instance or static components. Every
method can trigger the events of its class.
● Event Handler
A event handler is a method of another class or the same class that
can handle an event.
● Registration of handlers
Objects which contain the handler methods for an event can be
registered at runtime as handlers for the objects of the classes which
can trigger the event. Clonut Solutions Pvt Ltd
Example

Clonut Solutions Pvt Ltd


Thanks for watching

Clonut Solutions Pvt Ltd

You might also like