Skip to Content

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.

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

  1. 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,

                perimeter.

    ENDINTERFACE.

    *

    class lcl_square DEFINITION.

       PUBLIC SECTION.

         INTERFACEs: lif_shape.

    ENDCLASS.

    STARTOFSELECTION.

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

       ENDMETHOD.

       method lif_shape~perimeter.

         write: /‘Square Perimeter calcuated’.

       ENDMETHOD.

    ENDCLASS.

    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

    Regards,
    Naimesh Patel

    (0) 
    1. VR V 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

      Venkat

      (0) 
  2. akira jain

    Example:

    Report ZINTERFACE1.
    INTERFACE my_interface1.
    Methods msg.
    ENDINTERFACE.

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

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

    Method add_number.
    ADD 7 TO num.
    EndMethod.
    ENDCLASS.

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

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

    Method speed1.
    Add 4 To wheel1.
    EndMethod.
    ENDCLASS.

    Start-Of-Selection.
    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: http://sapabapcentral.blogspot.in/p/142-sap-abap-interfaces.html

    (0) 

Leave a Reply