Hi all,
In this document i want to explain the general and simple example on Interfaces concept of OOABAP.
Before going to the example let me summarize few points on Interfaces.
Let us take an example of Rectangle and Square Interfaces which contains methods Area and Perimeter.
So our objective is to calculate the area and perimeters of square and rectangle using Interface concept.
"Rectangle interface declaration
INTERFACE Rectangle.
"Components declaration"
ENDINTERFACE.
"Square Interface declaration
INTERFACE Square.
"components declaration"
ENDINTERFACE.
"Class Definition
This lcl_interface class declares the two interfaces square and rectangle.
Interfaces are declared again in this local class
CLASS lcl_interface definition.
"Components declaration"
endclass.
"Class Implementation
lcl_interface implements the methods Area and Perimeter of two Interfaces Square and Rectangle.
Since all the methods of the Interface are implemented lcl_interface cannot be a Abstract class.
class lcl_interface IMPLEMENTATION.
method rectangle~area.
res = rectangle~length * rectangle~breadth.
write: / res.
endmethod.
method rectangle~perimeter.
res = 2 * ( rectangle~length + rectangle~breadth ).
write: / res.
ENDMETHOD.
METHOD square~area.
res = square~side * square~side.
write:/ res.
ENDMETHOD.
method square~perimeter.
res = 2 * square~side.
write: / res.
ENDMETHOD.
ENDCLASS.
create object ob.
"Calling the methods of interfaces through the object of class lcl_interface.
call METHOD: ob->rectangle~area,
ob->rectangle~perimeter,
ob->square~area,
ob->square~perimeter.
For the detail code find the attachment.