SAP ABAP OBJECTS Tutorials

Interfaces

In ABAP interfaces are implemented in addition to, and independently of classes. An interface only has a declaration part, and do not have visibillity 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 carying out the implementation.

·                Interface components are adresse 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 triggerede, only those events handlers that have registred 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>]

Define, implement and use simple class

***INCLUDE ZBC404_HF_LCL_AIRPLANE .

******************************************

* Definition part

******************************************

CLASS lcl_airplane DEFINITION.

*——————————

* Public section

*——————————

PUBLIC SECTION.

TYPES: t_name(25) TYPE c.

METHODS:

constructor,

set_attributes IMPORTING p_name       TYPE t_name

p_planetype  TYPE saplane-planetype,

display_attributes,

display_n_o_airplanes.

*——————————

* Private section

*——————————

PRIVATE SECTION.

*   Private attributes

DATA: name(25) TYPE c,

planetype TYPE saplane-planetype.

*   Private static attribute

CLASS-DATA n_o_airplanes TYPE i.

ENDCLASS.

******************************************

* Implementation part

******************************************

CLASS lcl_airplane IMPLEMENTATION.

METHOD constructor.

*   Counts number of instances

n_o_airplanes = n_o_airplanes + 1.

ENDMETHOD.

METHOD set_attributes.

name      = p_name.

planetype = p_planetype.

ENDMETHOD.

METHOD display_attributes.

WRITE:/ ‘Name:’, name, ‘Planetype:’, planetype.

ENDMETHOD.

METHOD display_n_o_airplanes.

WRITE: / ‘No. planes:’, n_o_airplanes.

ENDMETHOD.

ENDCLASS.

REPORT zbc404_hf_maintain_airplanes .

INCLUDE zbc404_hf_lcl_airplane.

* Create reference to class lcl_airplane

DATA: airplane1 TYPE REF TO lcl_airplane,

airplane2 TYPE REF TO lcl_airplane.

START-OF-SELECTION.

* Create instance

CREATE OBJECT airplane1.

CALL METHOD: airplane1->display_n_o_airplanes.

CREATE OBJECT airplane2.

* Setting attributes using a method with parameters

CALL METHOD airplane1->set_attributes EXPORTING p_name      = ‘Kurt’

p_planetype = ‘MD80’.

END-OF-SELECTION.

* Using methods

CALL METHOD: airplane1->display_n_o_airplanes,

airplane1->display_attributes.

The resulting report:

Maintain airplanes

No. planes:          1

No. planes:          2

Name: Kurt                      Planetype: MD80

Use constructor to create an object with parameters

CLASS lcl_airplane DEFINITION.

PUBLIC SECTION.

TYPES: t_name(25) TYPE c.

METHODS:

constructor    importing p2_name      type t_name

p2_planetype  TYPE saplane-planetype,

….. more code …….

CLASS lcl_airplane IMPLEMENTATION.

METHOD constructor.

name      = p2_name.

planetype = p2_planetype.

….. more code …….

START-OF-SELECTION.

CREATE OBJECT airplane1 exporting p2_name = ‘Hansemand’

p2_planetype = ‘Boing 747’.

Subclassing

This example uses a superclass LCL_AIRPLANE and subclasses it into LCL_PASSENGER_AIRPLANE and LCL_CARGO_PLANE.

LCL_AIRPLANE has a method display_n_o_airplanes that displays the number of object instances.

LCL_PASSENGER_AIRPLANE has the private instance attribute n_o_seats, and redefines the superclass method display_attributes, so it also displays n_o_seats.

LCL_CARGO_PLANE has the private instance attribute cargomax, and redefines the superclass method display_attributes, so it also displays cargomax.

All instance attributes are provided by the cunstructor method.

Superclass LCL_AIRPLANE

***INCLUDE ZBC404_HF_LCL_AIRPLANE .

******************************************

* Definition part

******************************************

CLASS lcl_airplane DEFINITION.

*——————————

* Public section

*——————————

PUBLIC SECTION.

TYPES: t_name(25) TYPE c.

METHODS:

constructor    IMPORTING im_name      TYPE t_name

im_planetype  TYPE saplane-planetype,

display_attributes.

*   Static methods

CLASS-METHODS:

display_n_o_airplanes.

*——————————

* Protected section

*——————————

PROTECTED SECTION.

*   Private attributes

DATA: name(25) TYPE c,

planetype TYPE saplane-planetype.

*   Private static attribute

CLASS-DATA n_o_airplanes TYPE i.

ENDCLASS.

******************************************

* Implementation part

******************************************

CLASS lcl_airplane IMPLEMENTATION.

METHOD constructor.

name      = im_name.

planetype = im_planetype.

*   Counts number of instances

n_o_airplanes = n_o_airplanes + 1.

ENDMETHOD.

METHOD display_attributes.

WRITE:/ ‘Name:’, name, ‘Planetype:’, planetype.

ENDMETHOD.

METHOD display_n_o_airplanes.

WRITE: / ‘No. planes:’, n_o_airplanes.

ENDMETHOD.

ENDCLASS.

Sub class LCL_PASSENGER_AIRPLANE

***INCLUDE ZBC404_HF_LCL_PASSENGER_PLANE .

*******************************************************************

* This is a subclass of class lcl_airplane

*******************************************************************

CLASS lcl_passenger_airplane DEFINITION INHERITING FROM lcl_airplane.

PUBLIC SECTION.

*   The constructor contains the parameters from the superclass

*   plus the parameters from the subclass

METHODS:

constructor IMPORTING im_name      TYPE t_name

im_planetype TYPE saplane-planetype

im_n_o_seats TYPE sflight-seatsmax,

*     Redefinition of superclass method display_attributes

display_attributes REDEFINITION.

PRIVATE SECTION.

DATA: n_o_seats TYPE sflight-seatsmax.

ENDCLASS.

CLASS lcl_passenger_airplane IMPLEMENTATION.

METHOD constructor.

*   The constructor method of the superclass MUST be called withing the

*   construtor

CALL METHOD super->constructor

EXPORTING im_name      = im_name

im_planetype = im_planetype.

n_o_seats = im_n_o_seats.

ENDMETHOD.

*   The redefined  display_attributes method

METHOD display_attributes.

CALL METHOD super->display_attributes.

WRITE: / ‘No. seats:’, n_o_seats.

ENDMETHOD.

ENDCLASS.

Sub class LCL_CARGO_PLANE

***INCLUDE ZBC404_HF_LCL_CARGO_PLANE .

*******************************************************************

* This is a subclass of class lcl_airplane

*******************************************************************

CLASS lcl_cargo_plane DEFINITION INHERITING FROM lcl_airplane.

PUBLIC SECTION.

METHODS:

*     The constructor contains the parameters from the superclass

*     plus the parameters from the subclass

constructor IMPORTING im_name      TYPE t_name

im_planetype TYPE saplane-planetype

im_cargomax  type scplane-cargomax,

*     Redefinition of superclass method display_attributes

display_attributes REDEFINITION.

PRIVATE SECTION.

DATA:cargomax TYPE scplane-cargomax.

ENDCLASS.

CLASS lcl_cargo_plane IMPLEMENTATION.

METHOD constructor.

*   The constructor method of the superclass MUST be called withing the

*   constructor

CALL METHOD super->constructor

EXPORTING im_name      = im_name

im_planetype = im_planetype.

cargomax = im_cargomax.

ENDMETHOD.

METHOD display_attributes.

*   The redefined  display_attributes method

CALL METHOD super->display_attributes.

WRITE: / ‘Cargo max:’, cargomax.

ENDMETHOD.

ENDCLASS.

The Main program that uses the classes

REPORT zbc404_hf_main .

* Super class

INCLUDE zbc404_hf_lcl_airplane.

* Sub classes

INCLUDE zbc404_hf_lcl_passenger_plane.

INCLUDE zbc404_hf_lcl_cargo_plane.

DATA:

* Type ref to sub classes. Note: It is not necesssary to make typeref to the superclass

o_passenger_airplane TYPE REF TO lcl_passenger_airplane,

o_cargo_plane        TYPE REF TO lcl_cargo_plane.

START-OF-SELECTION.

* Display initial number of instances = 0

CALL METHOD  lcl_airplane=>display_n_o_airplanes.

* Create objects

CREATE OBJECT o_passenger_airplane

EXPORTING

im_name      = ‘LH505’

im_planetype = ‘Boing 747’

im_n_o_seats = 350.

CREATE OBJECT o_cargo_plane

EXPORTING

im_name      = ‘AR13’

im_planetype = ‘DC 3’

im_cargomax = 35.

* Display attributes

CALL METHOD o_passenger_airplane->display_attributes.

CALL METHOD o_cargo_plane->display_attributes.

* Call static method display_n_o_airplanes

* Note: The syntax for calling a superclass method, differs from the syntax when calling a subclass method.

* When calling a superclass => must be used instead of ->

CALL METHOD  lcl_airplane=>display_n_o_airplanes.

Result:

No. planes: 0
Name: LH505 Planetype: Boing 747
No. seats: 350
Name: AR13 Planetype: DC 3
Cargo max: 35,0000
No. planes: 2

Print Friendly, PDF & Email

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