Vous êtes sur la page 1sur 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 Accessed By Stored in Created by Namespace Any program In the class repository Created using Tcode SE24 Must begin with Y or Z Local Class Only in the program where it is defined Only in the program where it is defined Created using Tcode SE38 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. PRIVATE SECTION. CLASS-DATA: TAStack TYPE REF TO TACtrl OCCURS 0. ENDCLASS. CLASS TACtrl IMPLEMENTATION. METHOD CreateNew. DATA NewTA TYPE REF TO TACtrl. -- instance methods

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
EVENTS event [ EXPORTING

Create a handler method in the program


...<list of export parameters> ].

Event Handling Example


Sender
*---- proxy class for GUI control CLASS CButton DEFINITION. PUBLIC SECTION. METHODS: SetLabel IMPORTING Txt TYPE . EVENTS: Clicked EXPORTING DoubleClick TYPE I. ENDCLASS.

Handler
CLASS CWindow1 DEFINITION. PUBLIC SECTION. "--- handle events by implementing "--- event handler methods METHODS: OKClicked FOR EVENT Clicked OF CButton IMPORTING DoubleClick, CanClicked FOR EVENT Clicked OF CButton. DATA: OKBtn TYPE REF TO CButton. ENDCLASS. CLASS CWindow1 IMPLEMENTATION. METHOD Init. CREATE OBJECT: OKBtn, CanBtn. SET HANDLER: OKClicked FOR OKBtn, CanClicked FOR CanBtn. ENDMETHOD. METHOD OKClicked. IF DoubleClick = 1. ENDMETHOD.

CLASS CButton IMPLEMENTATION. METHOD AnyMethod. RAISE EVENT Clicked EXPORTING DoubleClick = 0. ENDMETHOD. ENDCLASS.

ENDIF.

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

Vous aimerez peut-être aussi