You are on page 1of 17

OO ABAP Definitons

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

Public attributes
Private attributes
Instance attributes
Static attributes
Public methods
Private methods
Constructor method
Static constructor
Protected components
Polymorphism

Public attributes
Public attributes are defined in the PUBLIC section and can be viewed and changed from outside the class. There is
direct access to public attributes. As a general rule, as few public attributes should be defined as possible.
PUBLIC SECTION.
DATA: Counter type i.

Private attributes
Private attributes are defined in the PRIVATE section. The can only be viewes and changed from within the class.
There is no direct access from outside the class.
PRIVATE SECTION.
DATA: name(25) TYPE c,
planetype LIKE saplane-planetyp,

Instance attributes
There exist one instance attribute for each instance of the class, thus they exist seperately for each object. Instance
attributes are declared with the DATA keyword.

Static attributes
Static attributes exist only once for each class. The data are the same for all instances of the class, and can be used
e.g. for instance counters. Static attributes are defined with the keyword CLASS-DATA.
PRIVATE SECTION.
CLASS-DATA: counter type i,

Public methods
Can called from outside the class
PUBLIC SECTION.
METHODS:

set_attributes IMPORTING p_name(25) TYPE c,


p_planetype LIKE

saplane-planetyp,

Private methods
Can only be called from inside the class. They are placed in the PRIVATE section of the class.

Constructor method
Implicitly, each class has an instance constructor method with the reserved name constructor and a static constructor
method with the reserved name class_constructor.
The instance constructor is executed each time you create an object (instance) with the CREATE OBJECT
statement, while the class constructor is executed exactly once before you first access a class.
The constructors are always present. However, to implement a constructor you must declare it explicitly with the
METHODS or CLASS-METHODS statements. An instance constructor can have IMPORTING parameters and
exceptions. You must pass all non-optional parameters when creating an object. Static constructors have no
parameters.

Static constructor
The static constructor is always called CLASS_CONSTRUCTER, and is called autmatically before the clas is first
accessed, that is before any of the following actions are executed:
Creating an instance using CREATE_OBJECT
Adressing a static attribute using <classname>-><attrbute>
Calling a ststic attribute using CALL METHOD
Registering a static event handler
Registering an evetm handler method for a static event
The static constructor cannot be called explicitly.

Protected components
When we are talking subclassing and enheritance there is one more component than Public and Private, the
Protected component. Protected components can be used by the superclass and all of the subclasses. Note that
Subclasses cannot access Private components.

Polymorphism
Polymorphism: When the same method is implemented differently in different classes. This can be done using
enheritance, by redefining a method from the superclass in subclasses and implement it differently.

OO ABAP Syntax
Template for making a class | Template for calling a class | Subclass | Template for calling a class | Using af class as
a parameter for a method | Interfaces | Events

Template for making a class


Delete the parts that should not be used
******************************************
* Definition part
******************************************
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. Also accessable by subclasses
*--------------------------------------------------PROTECTED SECTION.
*--------------------------------------------------* Private section. Not accessable by subclasses
*--------------------------------------------------PRIVATE SECTION.
ENDCLASS.
******************************************
* Implementation part
******************************************
CLASS lcl_airplane IMPLEMENTATION.
METHOD constructor.
ENDMETHOD.
METHOD mm1.
ENDMETHOD.
METHOD mm2.
ENDMETHOD.
ENDCLASS.

Template for calling a class


* Create reference to class lcl_airplane
DATA: airplane1 TYPE REF TO lcl_airplane.
START-OF-SELECTION.
* Create instance using parameters in the cosntructor method
CREATE OBJECT airplane1 exporting im_name = 'Hansemand'
im_planetype = 'Boing 747'.
* Calling a method with parameters
CALL METHOD: airplane1->display_n_o_airplanes,
airplane1->display_attributes.

Subclass
CLASS xxx DEFINITION INHERITING FROM yyy.

Using af class as a parameter for a method


The class LCL_AIRPLANE is used as a parameter for method add_a_new_airplane:
METHODS:
add_a_new_airplane importing im_airplane TYPE REF to lcl_airplane.

Interfaces
In ABAP interfaces are implemented in addition to, and independently of classes. An interface only has a declaration
part,
and do not have visibility sections. Components (Attributes, methods, constants, types) can be defined the same way
as in classes.

Interfaces are listed in the definition part lof the class,


and must always be in the PUBLIC SECTION.

Operations defined in the interface atre impemented as


methods of the class. All methods of the interface
must be present in the implementation part of the class.

Attributes, events, constants and types defined in the


interface are automatically available to the class
carrying out the implementation.

Interface components are addressed in the class by <interface


name>~<component name>
Example of how to implement an interface:
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.
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.
CALL METHOD text_doc->lif_document~display.
CALL METHOD text_doc->display.

Events
For events of controls, refer to How to use controls.

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 derigert it by using ACTIVATION 'SPACE'.
Defining events:
CLASS <classname> DEFINITION.
EVENTS: <event> EXPORTING VALUE (<ex_par>) TYPE type.
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>]

OO ABAP Step by Step Example


This example is a step by step example, moving from a simple clas to more sophisticated classes, and covers
inheritance, interfaces and events.
Simple class | Inheritance and ploymorphism | Interfaces | Events
1. Simple class
This example shows how to create a simple employee class. The constructor method is used to initialize number and
name of thje employee when the object is created. A display_employee method can be called to show the attributes
of the employee, and CLASS-METHOD dosplay_no_of_employees can be called to show the total number of
employees (Number of instances of the employee class).
REPORT zbc404_hf_events_1.
*********************************************************************
* L C L _ E M P L O Y E E
*********************************************************************
*---- LCL Employee - Definition
CLASS lcl_employee DEFINITION.
PUBLIC SECTION.
*-------------------------------------------------------------------* The public section is accesible from outside
*-------------------------------------------------------------------TYPES:
BEGIN OF t_employee,
no TYPE i,
name TYPE string,
END OF t_employee.
METHODS:
constructor
IMPORTING im_employee_no TYPE i
im_employee_name TYPE string,
display_employee.
*
Class methods are global for all instances
CLASS-METHODS: display_no_of_employees.
PROTECTED SECTION.
*-------------------------------------------------------------------* The protecetd section is accesible from the class and its subclasses
*-------------------------------------------------------------------*
Class data are global for all instances
CLASS-DATA: g_no_of_employees TYPE i.
PRIVATE SECTION.
*-------------------------------------------------------------------* The private section is only accesible from within the classs
*-------------------------------------------------------------------DATA: g_employee TYPE t_employee.
ENDCLASS.
*--- LCL Employee - Implementation
CLASS lcl_employee IMPLEMENTATION.

METHOD constructor.
g_employee-no = im_employee_no.
g_employee-name = im_employee_name.
g_no_of_employees = g_no_of_employees + 1.
ENDMETHOD.
METHOD display_employee.
WRITE:/ 'Employee', g_employee-no, g_employee-name.
ENDMETHOD.
METHOD display_no_of_employees.
WRITE: / 'Number of employees is:', g_no_of_employees.
ENDMETHOD.
ENDCLASS.
************************************************************************
* R E P O R T
*********************************************************************
DATA: g_employee1 TYPE REF TO lcl_employee,
g_employee2 TYPE REF TO lcl_employee.
START-OF-SELECTION.
CREATE OBJECT g_employee1
EXPORTING im_employee_no = 1
im_employee_name = 'John Jones'.
CREATE OBJECT g_employee2
EXPORTING im_employee_no = 2
im_employee_name = 'Sally Summer'.
CALL METHOD g_employee1->display_employee.
CALL METHOD g_employee2->display_employee.
CALL METHOD g_employee2->display_no_of_employees.

2. Inheritance and 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 calcukated in the method add_employee, but as the wages are calculated differently for blue collar
employees and white collar emplyees, the superclass method add_employee is redeifined in the subclasses.
Principles:
Create super class LCL_CompanyEmployees.
The class has the methods:
Constructor
Add_Employee - Adds a new employee to the list of employees
Display_Employee_List - Displays all employees and there wage
Display_no_of_employees - Displays total number of employees
Note the use of CLASS-DATA to keep the list of employees and number of employees the same from instance to
instance.
Create subclasses lcl_bluecollar_employee and lcl_whitecollar_employee. The calsses are identical, except for the
redifinition of the add_employee method, where the caclculation of wage is different.
Methodes:
Constructor. The constructor is used to initialize the attributes of the employee. Note that the constructor in
the supclasss has to be called from within the constructor of the subclass.
Add_Employee. This is a redinition of the same method in the superclass. In the redefined class the wage is
calcuated, and the superclass method is called to add the employees to the emploee list.:
The program
REPORT zbc404_hf_events_2 .
*******************************************************

* Super class LCL_CompanyEmployees


*******************************************************
CLASS lcl_company_employees DEFINITION.
PUBLIC SECTION.
TYPES:
BEGIN OF t_employee,
no TYPE i,
name TYPE string,
wage TYPE i,
END OF t_employee.
METHODS:
constructor,
add_employee
IMPORTING im_no
TYPE i
im_name TYPE string
im_wage TYPE i,
display_employee_list,
display_no_of_employees.
PRIVATE SECTION.
CLASS-DATA: i_employee_list TYPE TABLE OF t_employee,
no_of_employees TYPE i.
ENDCLASS.
*-- CLASS LCL_CompanyEmployees IMPLEMENTATION
CLASS lcl_company_employees IMPLEMENTATION.
METHOD constructor.
no_of_employees = no_of_employees + 1.
ENDMETHOD.
METHOD add_employee.
*
Adds a new employee to the list of employees
DATA: l_employee TYPE t_employee.
l_employee-no = im_no.
l_employee-name = im_name.
l_employee-wage = im_wage.
APPEND l_employee TO i_employee_list.
ENDMETHOD.
METHOD display_employee_list.
*
Displays all employees and there wage
DATA: l_employee TYPE t_employee.
WRITE: / 'List of Employees'.
LOOP AT i_employee_list INTO l_employee.
WRITE: / l_employee-no, l_employee-name, l_employee-wage.
ENDLOOP.
ENDMETHOD.
METHOD display_no_of_employees.
*
Displays total number of employees
SKIP 3.
WRITE: / 'Total number of employees:', no_of_employees.
ENDMETHOD.
ENDCLASS.
*******************************************************

* Sub class LCL_BlueCollar_Employee


*******************************************************
CLASS lcl_bluecollar_employee DEFINITION
INHERITING FROM lcl_company_employees.
PUBLIC SECTION.
METHODS:
constructor
IMPORTING im_no
TYPE i
im_name
TYPE string
im_hours
TYPE i
im_hourly_payment TYPE i,
add_employee REDEFINITION.
PRIVATE SECTION.
DATA:no
TYPE i,
name
TYPE string,
hours
TYPE i,
hourly_payment TYPE i.
ENDCLASS.
*---- CLASS LCL_BlueCollar_Employee IMPLEMENTATION
CLASS lcl_bluecollar_employee IMPLEMENTATION.
METHOD constructor.
*
The superclass constructor method must be called from the subclass
*

constructor method
CALL METHOD super->constructor.
no = im_no.
name = im_name.
hours = im_hours.
hourly_payment = im_hourly_payment.
ENDMETHOD.
METHOD add_employee.
*
Calculate wage an call the superclass method add_employee to add
*

the employee to the employee list


DATA: l_wage TYPE i.
l_wage = hours * hourly_payment.
CALL METHOD super->add_employee
EXPORTING im_no = no
im_name = name
im_wage = l_wage.
ENDMETHOD.
ENDCLASS.
*******************************************************
* Sub class LCL_WhiteCollar_Employee
*******************************************************
CLASS lcl_whitecollar_employee DEFINITION
INHERITING FROM lcl_company_employees.
PUBLIC SECTION.
METHODS:

constructor
IMPORTING im_no
TYPE i
im_name
TYPE string
im_monthly_salary
TYPE i
im_monthly_deductions TYPE i,
add_employee REDEFINITION.
PRIVATE SECTION.
DATA:
no
TYPE i,
name
TYPE string,
monthly_salary
TYPE i,
monthly_deductions
TYPE i.
ENDCLASS.
*---- CLASS LCL_WhiteCollar_Employee IMPLEMENTATION
CLASS lcl_whitecollar_employee IMPLEMENTATION.
METHOD constructor.
*
The superclass constructor method must be called from the subclass
*

constructor method

CALL METHOD super->constructor.


no = im_no.
name = im_name.
monthly_salary = im_monthly_salary.
monthly_deductions = im_monthly_deductions.
ENDMETHOD.
METHOD add_employee.
*
Calculate wage an call the superclass method add_employee to add
*

the employee to the employee list


DATA: l_wage TYPE i.
l_wage = monthly_salary - monthly_deductions.
CALL METHOD super->add_employee
EXPORTING im_no = no
im_name = name
im_wage = l_wage.
ENDMETHOD.
ENDCLASS.
*******************************************************
* R E P O R T
*******************************************************
DATA:
* Object references
o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee,
o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee.
START-OF-SELECTION.
* Create bluecollar employee obeject
CREATE OBJECT o_bluecollar_employee1
EXPORTING im_no = 1
im_name = 'Gylle Karen'
im_hours = 38
im_hourly_payment = 75.
* Add bluecollar employee to employee list
CALL METHOD o_bluecollar_employee1->add_employee
EXPORTING im_no = 1

im_name = 'Gylle Karen'


im_wage = 0.
* Create whitecollar employee obeject
CREATE OBJECT o_whitecollar_employee1
EXPORTING im_no = 2
im_name = 'John Dickens'
im_monthly_salary = 10000
im_monthly_deductions = 2500.
* Add bluecollar employee to employee list
CALL METHOD o_whitecollar_employee1->add_employee
EXPORTING im_no = 1
im_name = 'Karen Johnson'
im_wage = 0.
* Display employee list and number of employees. Note that the result
* will be the same when called from o_whitecollar_employee1 or
* o_bluecolarcollar_employee1, because the methods are defined
* as static (CLASS-METHODS)
CALL METHOD o_whitecollar_employee1->display_employee_list.
CALL METHOD o_whitecollar_employee1->display_no_of_employees.
The resulting report
List of Employees
1 Karen Johnson 2.850
2 John Dickens 7.500
Total number of employees: 2

3. Interfaces
This example is similiar to th eprevious example, however an interface is implemented with the method
add_employee. Note that the interface is only implemented in the superclass ( The INTERFACE stament), but also
used in the subclasses.
The interface in the example only contains a method, but an iterface can also contain attrbutes, constants, types and
alias names.
The output from example 3 is similiar to the output in example 2.
All changes in the program compared to example 2 are marked with red.
REPORT zbc404_hf_events_3 .
*---------------------------------------------------------------------*
*

INTERFACE lif_employee

*---------------------------------------------------------------------*
INTERFACE lif_employee.
METHODS:
add_employee
IMPORTING im_no

TYPE i

im_name TYPE string


im_wage TYPE i.

ENDINTERFACE.
*******************************************************
* Super class LCL_CompanyEmployees
*******************************************************
CLASS lcl_company_employees DEFINITION.
PUBLIC SECTION.
INTERFACES lif_employee.
TYPES:
BEGIN OF t_employee,
no TYPE i,
name TYPE string,
wage TYPE i,
END OF t_employee.
METHODS:
constructor,
*
add_employee
"Removed
IMPORTING im_no
TYPE i
im_name TYPE string
im_wage TYPE i,
display_employee_list,
display_no_of_employees.
PRIVATE SECTION.
CLASS-DATA: i_employee_list TYPE TABLE OF t_employee,
no_of_employees TYPE i.
ENDCLASS.
*-- CLASS LCL_CompanyEmployees IMPLEMENTATION
CLASS lcl_company_employees IMPLEMENTATION.
METHOD constructor.
no_of_employees = no_of_employees + 1.
ENDMETHOD.
METHOD lif_employee~add_employee.
*
Adds a new employee to the list of employees
DATA: l_employee TYPE t_employee.
l_employee-no = im_no.
l_employee-name = im_name.
l_employee-wage = im_wage.
APPEND l_employee TO i_employee_list.
ENDMETHOD.
METHOD display_employee_list.
*
Displays all employees and there wage
DATA: l_employee TYPE t_employee.
WRITE: / 'List of Employees'.
LOOP AT i_employee_list INTO l_employee.
WRITE: / l_employee-no, l_employee-name, l_employee-wage.
ENDLOOP.
ENDMETHOD.
METHOD display_no_of_employees.
*
Displays total number of employees
SKIP 3.
WRITE: / 'Total number of employees:', no_of_employees.

ENDMETHOD.
ENDCLASS.
*******************************************************
* Sub class LCL_BlueCollar_Employee
*******************************************************
CLASS lcl_bluecollar_employee DEFINITION
INHERITING FROM lcl_company_employees.
PUBLIC SECTION.
METHODS:
constructor
IMPORTING im_no
TYPE i
im_name
TYPE string
im_hours
TYPE i
im_hourly_payment TYPE i,
lif_employee~add_employee REDEFINITION..
PRIVATE SECTION.
DATA:no
TYPE i,
name
TYPE string,
hours
TYPE i,
hourly_payment TYPE i.
ENDCLASS.
*---- CLASS LCL_BlueCollar_Employee IMPLEMENTATION
CLASS lcl_bluecollar_employee IMPLEMENTATION.
METHOD constructor.
*
The superclass constructor method must be called from the subclass
*

constructor method

CALL METHOD super->constructor.


no = im_no.
name = im_name.
monthly_salary = im_monthly_salary.
monthly_deductions = im_monthly_deductions.
ENDMETHOD.
METHOD lif_employee~add_employee.
*
Calculate wage an call the superclass method add_employee to add
*

the employee to the employee list


DATA: l_wage TYPE i.
l_wage = monthly_salary - monthly_deductions.
CALL METHOD super->lif_employee~add_employee
EXPORTING im_no = no
im_name = name
im_wage = l_wage.
ENDMETHOD.
ENDCLASS.
*******************************************************
* R E P O R T

*******************************************************
DATA:
* Object references
o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee,
o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee.
START-OF-SELECTION.
* Create bluecollar employee obeject
CREATE OBJECT o_bluecollar_employee1
EXPORTING im_no = 1
im_name = 'Gylle Karen'
im_hours = 38
im_hourly_payment = 75.
* Add bluecollar employee to employee list
CALL METHOD o_bluecollar_employee1->lif_employee~add_employee
EXPORTING im_no = 1
im_name = 'Karen Johnson'
im_wage = 0.
* Create whitecollar employee obeject
CREATE OBJECT o_whitecollar_employee1
EXPORTING im_no = 2
im_name = 'John Dickens'
im_monthly_salary = 10000
im_monthly_deductions = 2500.
* Add bluecollar employee to employee list
CALL METHOD o_whitecollar_employee1->lif_employee~add_employee
EXPORTING im_no = 1
im_name = 'Gylle Karen'
im_wage = 0.
* Display employee list and number of employees. Note that the result
* will be the same when called from o_whitecollar_employee1 or
* o_bluecolarcollar_employee1, because the methods are defined
* as static (CLASS-METHODS)
CALL METHOD o_whitecollar_employee1->display_employee_list.
CALL METHOD o_whitecollar_employee1->display_no_of_employees.

4. Events
This is the same example as example 4. All changes are marked with red. There have been no canges to the
subclasses, only to the superclass and the report, sp the code for th esubclasses is not shown.
For a simple example refer to Events in Examples.
REPORT zbc404_hf_events_4 .
*---------------------------------------------------------------------*
*

INTERFACE lif_employee

*---------------------------------------------------------------------*
INTERFACE lif_employee.
METHODS:
add_employee
IMPORTING im_no
TYPE i
im_name TYPE string
im_wage TYPE i.
ENDINTERFACE.

*******************************************************
* Super class LCL_CompanyEmployees
*******************************************************
CLASS lcl_company_employees DEFINITION.
PUBLIC SECTION.
TYPES:
BEGIN OF t_employee,
no TYPE i,
name TYPE string,
wage TYPE i,
END OF t_employee.
*
Declare event. Note that declaration could also be placed in the
*

interface
EVENTS: employee_added_to_list
EXPORTING value(ex_employee_name) TYPE string.

CLASS-EVENTS: Events can also be defined as class-events


INTERFACES lif_employee.
METHODS:
constructor,
display_employee_list,
display_no_of_employees,
Declare event method

on_employee_added_to_list FOR EVENT employee_added_to_list OF


lcl_company_employees
IMPORTING ex_employee_name sender.
PRIVATE SECTION.
CLASS-DATA:
i_employee_list TYPE TABLE OF t_employee,
no_of_employees TYPE i.
ENDCLASS.
*-- CLASS LCL_CompanyEmployees IMPLEMENTATION
CLASS lcl_company_employees IMPLEMENTATION.
METHOD constructor.
no_of_employees = no_of_employees + 1.
ENDMETHOD.
METHOD lif_employee~add_employee.
*
Adds a new employee to the list of employees

DATA: l_employee TYPE t_employee.


l_employee-no = im_no.
l_employee-name = im_name.
l_employee-wage = im_wage.
APPEND l_employee TO i_employee_list.
Raise event employee_added_to_list
RAISE EVENT employee_added_to_list

EXPORTING ex_employee_name = l_employee-name.


ENDMETHOD.
METHOD display_employee_list.
*
Displays all employees and there wage

DATA: l_employee TYPE t_employee.


WRITE: / 'List of Employees'.
LOOP AT i_employee_list INTO l_employee.
WRITE: / l_employee-no, l_employee-name, l_employee-wage.
ENDLOOP.
ENDMETHOD.
METHOD display_no_of_employees.
*
Displays total number of employees
SKIP 3.
WRITE: / 'Total number of employees:', no_of_employees.
ENDMETHOD.
METHOD on_employee_added_to_list.
*

Event method
WRITE: / 'Employee added to list', ex_employee_name.

ENDMETHOD.
ENDCLASS.
*******************************************************
* Sub class LCL_BlueCollar_Employee
*******************************************************
CLASS lcl_bluecollar_employee DEFINITION
INHERITING FROM lcl_company_employees.
See code in example 3...
ENDCLASS.
CLASS lcl_bluecollar_employee IMPLEMENTATION.
See code in example 3...
ENDCLASS.
*******************************************************
* Sub class LCL_WhiteCollar_Employee
*******************************************************
CLASS lcl_whitecollar_employee DEFINITION
See code in example 3...
ENDCLASS.
CLASS lcl_whitecollar_employee IMPLEMENTATION.
See code in example 3...
ENDCLASS.
*******************************************************
* R E P O R T
*******************************************************
DATA:
* Object references
o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee,
o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee.
START-OF-SELECTION.
* Create bluecollar employee obeject

CREATE OBJECT o_bluecollar_employee1


EXPORTING im_no = 1
im_name = 'Karen Johnson'
im_hours = 38
im_hourly_payment = 75.
* Register event for o_bluecollar_employee1
SET HANDLER o_bluecollar_employee1->on_employee_added_to_list
FOR o_bluecollar_employee1.
* Add bluecollar employee to employee list
CALL METHOD o_bluecollar_employee1->lif_employee~add_employee
EXPORTING im_no = 1
im_name = 'Gylle Karen'
im_wage = 0.
* Create whitecollar employee obeject
CREATE OBJECT o_whitecollar_employee1
EXPORTING im_no = 2
im_name = 'John Dickens'
im_monthly_salary = 10000
im_monthly_deductions = 2500.
* Register event for o_whitecollar_employee1
SET HANDLER o_whitecollar_employee1->on_employee_added_to_list
FOR o_whitecollar_employee1.

* Add bluecollar employee to employee list


CALL METHOD o_whitecollar_employee1->lif_employee~add_employee
EXPORTING im_no = 1
im_name = 'Gylle Karen'
im_wage = 0.
* Display employee list and number of employees. Note that the result
* will be the same when called from o_whitecollar_employee1 or
* o_bluecolarcollar_employee1, because the methods are defined
* as static (CLASS-METHODS)
CALL METHOD o_whitecollar_employee1->display_employee_list.
CALL METHOD o_whitecollar_employee1->display_no_of_employees.
Result:
Employee added to list Karen Johnson
Employee added to list John Dickens
List of Employees
1 Karen Johnson 2.850
2 John Dickens 7.500
Total number of employees: 2

You might also like