Application Development and Automation Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Likes
17,599

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.

  • Interfaces are pure abstract classes. i.e, by default all the methods of interface are abstract.

  • By default, the visibility of interfaces is always public.

  • Creation of object is not possible since they are not implemented completely.

  • Class which implements the interface is an implementing class.

  • If a class implements the interface it has to re declare the interface in class definition using interface keyword.

  • The class which implements the interface must implement all the methods of interface otherwise the class should be declared as an Abstract class.

  • With the help of interface we can use multiple inheritance.

  • A class can implement multiple number of 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.

3 Comments