SAP ABAP OBJECTS Tutorials

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.

hours = im_hours.

hourly_payment = im_hourly_payment.

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 = hours * hourly_payment.

CALL METHOD super->lif_employee~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,

lif_employee~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 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

Print Friendly, PDF & Email

[ FREE ] SUBSCRIBE
RECEIVE UPDATES & SAP ARTICLES RIGHT IN YOUR INBOX.