Skip to Content
Author's profile photo Former Member

Example on Interfaces(OOABAP)

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”


“Square Interface declaration


  “components declaration”


“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”


“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.


    method rectangle~perimeter.

      res = 2 * ( rectangle~length + rectangle~breadth ).

      write: / res.


    METHOD square~area.

      res = square~side * square~side.

      write:/ res.


    method square~perimeter.

      res = 2 * square~side.

      write: / res.



  create object ob.

“Calling the methods of interfaces through the object of class lcl_interface.

call METHOD: ob->rectangle~area,




For the detail code find the attachment.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Naimesh Patel
      Naimesh Patel

      Hello Venkat,

      You mentioned:

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

      Interface are not at a CLASS. Interface is a separate entity and the class. Abstract class could have another methods which are not abstract and contain the Implementation. Whereas Interface just represent the semantic representation (design time) only. Read more on Abstract Class vs Interface - Which to use when? | ABAP Help Blog

      In your example code, you are somwhat incorrectly using the Interfaces. What you want to do is:

      • create one Interface like LIF_SHAPE with AREA, PERIMETER
      • Implement LIF_SHAPE into two classes, LCL_SQUARE and LCL_RECTANGLE
      • Instantiate the object for SQUARE and RECTANGLE.

      Something like this:

      INTERFACE lif_shape.

         methods: area,




      class lcl_square DEFINITION.


           INTERFACEs: lif_shape.



         data: lo_shape type ref to lif_shape.

         create OBJECT lo_shape type lcl_square.

         lo_shape->area( ).


      class lcl_square IMPLEMENTATION.

         method lif_shape~area.

           write: /'Square area calcuated'.


         method lif_shape~perimeter.

           write: /'Square Perimeter calcuated'.



      This way you can use any object which has LIF_SHAPE implemented to be used as LO_SHAPE.

      You also want to add few points here since you are listing for interfaces:

      • Interface are heavily used to achieve Polymorphism
      • You can use aliases for each method of the interface
      • While defining aliases, you can define the visibility of the component
      • You can use Interfaces to achieve the Multiple Inheritance

      Naimesh Patel

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Hi Naimesh,

      Thank you very much for your suggestions.

      Yes,i accept interfaces are seperate entity but not classes, inorder to compare with Abstract classes i used like that.

      In my example i used two interfaces and one class, here you used one interface and two classes.

      I can  add some more points as mentioned below in your reply but my main intention is to potray simple example on Interfaces...

      Anyway suggestions are always welcome

      Thank You


      Author's profile photo akira jain
      akira jain


      Report ZINTERFACE1.
      INTERFACE my_interface1.
      Methods msg.

      CLASS num_counter Definition.
      PUBLIC Section.
      INTERFACES my_interface1.
      Methods add_number.
      PRIVATE Section.
      Data num Type I.

      CLASS num_counter Implementation.
      Method my_interface1~msg.
      Write: / ‘The number is’, num.

      Method add_number.
      ADD 7 TO num.

      CLASS drive1 Definition.
      PUBLIC Section.
      INTERFACES my_interface1.
      Methods speed1.
      PRIVATE Section.
      Data wheel1 Type I.

      CLASS drive1 Implementation.
      Method my_interface1~msg.
      Write: / ‘Total number of wheels is’, wheel1.

      Method speed1.
      Add 4 To wheel1.

      Data object1 Type Ref To num_counter.
      Create Object object1.

      CALL Method object1→add_number.
      CALL Method object1→my_interface1~msg.

      Data object2 Type Ref To drive1.
      Create Object object2.

      CALL Method object2→speed1.
      CALL Method object2→my_interface1~msg.

      The above code produces the following output −

      The number is 7
      Total number of wheels is 4

      Note − The add_number and speed1 methods are specific to the respective classes.

      More Infomations: