You are on page 1of 16

Day 6

OOABAP

OBJECTIVE
This presentation aims at providing the target audience with the
following:

OO concepts
Overview of OOABAP

OO - CONCEPTS

Object
Class
Attributes
Encapsulation
Polymorphism
Inheritance

OOABAP - Attributes

Public

Private
Attributes

Instance

Static

OOABAP- Methods
Public

Private
Methods

Constructor

Static Constructor

CLASSES DEFINITION SYNTAX


CLASS xxx DEFINITION.
*Public Section
PUBLIC SECTION.
TYPES:
DATA:
*Static data
CLASS-DATA:
*Methods
METHODS:
*Using the constructor to initialize parameters
constructor IMPORTING xxx type yyy,
*Method with parameters
mm1 IMPORTING iii TYPE ttt.
*Method without parameters
mm2.
*Static methods
CLASS-METHODS:
*Protected Section
*Private Section
END CLASS

CLASSES IMPLEMENTATION SYNTAX


CLASS lcl_airplane
IMPLEMENTATION.
METHOD constructor.
ENDMETHOD.
METHOD mm1.
ENDMETHOD.
METHOD mm2.
ENDMETHOD.
ENDCLASS.

CLASSES

This example shows how to create a simple employee class. The


constructor method is used to initialize number and name of the
employee when the object is created. A display_employee method can
be called to show the attributes of the employee, and CLASS-METHOD
display_no_of_employees can be called to show the total number of
employees (Number of instances of the employee class).

SUB CLASSES
Declaration

CLASS xxx DEFINITION INHERITING FROM yyy.

An example for Sub Classes:

INHERITANCE & POLYMORPHISM

This example uses a superclass lcl_company_employees and two


subclasses lcl_bluecollar_employee and lcl_whitecollar_employee to add
employees to a list and then display a list of employees and there wages.
The wages are calculated in the method add_employee, but as the wages
are calculated differently for blue collar employees and white collar
employees, the superclass method add_employee is redefined in the
subclasses.

INTERFACES
Implemented :
In addition to & Independently of classes
Operations defined in the interface are implemented as methods of the
class, which must be present in class implementation part.
Interface attributes, events, constants and types are automatically
available to the class carrying out the implementation.
Interface components are addressed in the class by
<interface name> ~ <component name>
Definition:
Declaration (No visibility sections)
Components (Attributes, methods, constants, types) defined similar to
classes
Defined in public section of class definition

INTERFACES DEFINITION SYNTAX


Definition INTERFACE lif_document
DATA:
author type ref to lcl_author.
METHODS: print,
display.
ENDINTERFACE.
CLASS lcl_text_document DEFINITION.
PUBLIC SECTION.
INTERFACES lif_document.
METHODS display.
ENDCLASS.
Implementation CLASS lcl_text_document IMPLEMENTTION.
METHOD lif_document~print.
ENDMETHOD.
METHOD lif_document~display
ENDMETHOD.
METHOD display.
ENDMETHOD.
ENDCLASS.
REPORT zzz.
DATA: text_doc TYPE REF TO lcl_document.
Start-of-selection.
CREATE OBJECT text_doc.
CALL METHOD text_doc->lif_document~print.

INTERFACES
This example is similar to the previous example, however an interface
is implemented with the method add_employee. Note that the interface
is only implemented in the super class ( The INTERFACE statement),
but also used in the subclasses.
The interface in the example only contains a method, but an interface
can also contain attributes, constants, types and alias names.
The output from example 3 is similar to the output in example 2.
All changes in the program compared to example 2 are marked with
red.

EVENTS
Events can only have EXPORTING parameters
When an event is triggered, only those events handlers that have registered
themselves using SET HANDLER by this point of runtime are executed.
You can register an event using ACTIVATION 'X' and deregister it by using
ACTIVATION 'SPACE

EVENTS DEFINTION SYNTAX


Definition
CLASS <classname> DEFINITION.
EVENTS: <event> EXPORTING VALUE (<ex_par>) TYPE type.
Implementation
CLASS <classname> IMPLEMENTATION.
METHOD <m>:
RAISE EVENT <event> EXPORTING <ex_par> = <act_par>.
Handling events:
CLASS <classname> DEFINITION.
METHODS: <on_event> FOR <event> OF <classname> ! <interface> IMPORTING
<imp_par1>...<imp_parN> SENDER.
Setting handler
SET HANDLER <ref_handle> <on_event> FOR <ref_sender> ! FOR ALL
INSTANCES
[ACTIVATION <var>]

EVENTS

This is similar to the previous examples. All changes are marked with red.
There have been no changes to the subclasses, only to the super class and
the report, so the code for the subclasses is not shown.

You might also like