Oops Abap PDF

You might also like

You are on page 1of 22

OOPS ABAP

Pawan Kumar Akella


SAP ABAP Consultant
Intro
Attributes of OOPS
Inheritance
Encapsulation
Polymorphism
Abstraction
Fundamentals of OOPS
Classes
Objects
Methods
What is an Object ?
Objects have

state, described by its attributes

behavior, described by its methods

identity to distinguish them from other objects


with same state and behavior

Objects can interact with each other...

by accessing (public) attributes

by calling methods

by raising or handling events

Objects are instances of classes


Class
Structure of Class
Class contains components
Each component is assigned to Visibility section
Classes implement methods
Components of Class
Attributes
Methods
Events
Interfaces
Local Class vs. Global Class
Global Class Local Class
Accessed By Any program Only in the program
where it is defined
Stored in In the class repository Only in the program
where it is defined
Created by Created using Tcode Created using Tcode
SE24 SE38
Namespace Must begin with Y or Z Can begin with any
character
CLASS c1 DEFINITION.
PUBLIC SECTION.

DATA: v1 TYPE I,

o1 TYPE REF TO c1.


METHODS: m1 IMPORTING a1 TYPE REF TO c1,
m2 IMPORTING a1 TYPE REF TO c1
RETURNING result TYPE I.
PRIVATE SECTION.
DATA: v2 TYPE I.
ENDCLASS.

PROGRAM xy.
DATA o1 TYPE REF TO c1.

--- attribute can occur anywhere a normal variable can occur
CREATE OBJECT o1.
x = o1->v1 + sin( o1-> v1 ).
CALL FUNCTION 'abc' EXPORTING p1 = o1->v1 .

--- some method calls


CALL METHOD o1->m1 EXPORTING a1 = o1.
CALL METHOD o1->m1( o1 ). -- short form for 1 exporting arg

y = obj1->m2( x ). -- result can be used in expressions

Instance Vs Static Components
Instance Components exist separately in each
instance(object) of the class and are referred using
instance component selector ->
Static Components exists only once per class and
are valid for all instance of class. They are declared
with the CLASS keyword
Static components can be used without even
creating an Instance of the class and referred to
using static component selector =>
*--- class attribute definition
CLASS-DATA: var TYPE t .

*--- class method definition


CLASS-METHODS: cm <parameter syntax like methods>.
CLASS TACtrl DEFINITION.
PUBLIC SECTION.
--- class method to create new controller instance
CLASS-METHODS: CreateNew RETURNING TaObj TYPE REF TO TACtrl.
CLASS-DATA: Current TYPE REF TO TACtrl READ-ONLY.
METHODS: Commit, Abort. -- instance methods
PRIVATE SECTION.
CLASS-DATA: TAStack TYPE REF TO TACtrl OCCURS 0.
ENDCLASS.

CLASS TACtrl IMPLEMENTATION.


METHOD CreateNew.
DATA NewTA TYPE REF TO TACtrl.
CREATE OBJECT NewTA.
APPEND NewTA TO TAStack.
Current = NewTA.
ENDMETHOD.
ENDCLASS.

PROGRAM xy.
--- start nested transaction
CALL METHOD TACtrl=>CreateNew.

CALL METHOD TACtrl=>Current->Commit.
Visibility of Class
Public section
Protected section
Private section
Constructors
Constructors are special methods that cannot be
called using CALL METHOD instead they are
called automatically by the system
Executed only once per instance(object)
We have to define them explicitly in the class
They have a predefined name
Only has importing parameters and Exceptions
Two types of Constructors
Static Constructor
Instance Constructor
Instance Constructor
Class XYZ definition
METHODS:
* Using the constructor to initialize parameters
constructor IMPORTING xxx type yyy,.
Endclass.
Class xyz implementation.
Method Constructor.
Endmethod.
Endclass
START-OF-SELECTION.
CREATE OBJECT airplane1 exporting im_name = 'Hansemand'
im_planetype = 'Boing 747'.
Static Constructor
Class-methods: class_constructor
Only accessed once per program
Automatically called before the class is first
accessed
Creation of 1st object
1st access to static method
1st access to static attribute
Cannot have parameters or Exceptions
Inheritance
Inheritance allows you to derive a new class from
an existing class
Class ABC definition INHERITING FROM XYZ
XYZ is the super class and ABC is called as sub
class derived from super class
Only the public and protected components of the
super class are visible in the subclass
You can declare the private components in the
subclass that have the same name of private
components in super class. Each class works on
its own private components
Single Inheritance

More child class can be present to the parent class


Multiple Inheritance
Multiple Inheritance is not
supported in SAP ABAP
Inheritance
You can use REDEFINITION addition in methods
statement to redefine an inherited public or protected
instance method in sub class
The Method retains the same name and definition but
has new implementation.
This is called Method Overriding
To use the super class method functionality you can
use
super->methodname to access the redefined
method
Call method super->constructor to access super class
constructor
Abstract , Final & Singleton class
Addition of Abstract and final key words to class and
methods statements makes them Abstract and final
classes or methods
Abstract classes cannot be instantiated
An Abstract method is defined in a abstract class and
cannot be implemented in the same class it has to be
implemented in the sub class
If at least on method is abstract or not implemented then
class is called as Abstract Class
Final class
They cannot have subclasses and cannot be inherited
All methods in final class are by default Final
Singleton class
Classes that are both abstract and final are called
Singleton classes
Only static component can be used
Interfaces

Interfaces define the interaction between different


objects
Polymorphism independent of class / inheritance
Classes can implement multiple interfaces
Uniform access through interface reference

A rc h iv e M g r

IArchive

P la n
C u s to m e r M a te ria l
Interface
Interfaces are independent structures that you can
implement in a class to extend the scope of that
class
Interfaces, along with inheritance, provide
polymorphism.
A class with an interface should implement all the
methods of that interface
Values of interface attributes are assigned at the
time of inclusion in a class except
Nested interfaces is also possible
Interfaces
Events
Events occur at a particular point in time, e.g. change in
state of an object

Events can be raised to inform other interested objects

Events can pass parameters

To handle the Events

Create an event in the class

Create a triggering Method for the event in same /other


class

Create a handler method in the program


EVENTS event
[ EXPORTING ...<list of export parameters> ].
Event Handling Example
Sender Handler
CLASS CWindow1 DEFINITION.
PUBLIC SECTION.
"--- handle events by implementing
*---- proxy class for GUI control
"--- event handler methods
CLASS CButton DEFINITION.
METHODS:
PUBLIC SECTION. OKClicked FOR EVENT Clicked OF CButton
METHODS: SetLabel IMPORTING DoubleClick,
IMPORTING Txt TYPE . CanClicked FOR EVENT Clicked OF CButton.
EVENTS: Clicked DATA: OKBtn TYPE REF TO CButton.
EXPORTING DoubleClick TYPE I.
ENDCLASS. ENDCLASS.

CLASS CWindow1 IMPLEMENTATION.


CLASS CButton IMPLEMENTATION. METHOD Init.
CREATE OBJECT: OKBtn, CanBtn.
METHOD AnyMethod. SET HANDLER: OKClicked FOR OKBtn,
CanClicked FOR CanBtn.
RAISE EVENT Clicked ENDMETHOD.
EXPORTING DoubleClick = 0.
METHOD OKClicked.
ENDMETHOD. IF DoubleClick = 1. ENDIF.
ENDCLASS. ENDMETHOD.

METHOD CancelClicked.
"--- DoubleClick not visible
ENDMETHOD.
ENDCLASS.

You might also like