You are on page 1of 9

2/27/2020 Example on Interfaces(OOABAP) | SAP Blogs

Community

Ask a Question Write a Blog Post Login

Former Member
June 19, 2013 1 minute read

Example on Interfaces(OOABAP)
Follow RSS feed Like

0 Likes 6,601 Views 3 Comments

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.

https://blogs.sap.com/2013/06/19/example-on-interfacesooabap/ 1/9
2/27/2020 Example on Interfaces(OOABAP) | SAP Blogs

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.
https://blogs.sap.com/2013/06/19/example-on-interfacesooabap/ 2/9
2/27/2020 Example on Interfaces(OOABAP) | SAP Blogs

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

https://blogs.sap.com/2013/06/19/example-on-interfacesooabap/ 3/9
2/27/2020 Example on Interfaces(OOABAP) | SAP Blogs

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.

Alert Moderator

Assigned tags

ABAP Development |

Related Blog Posts

FRIENDSHIP in OOABAP
By Pramod Repaka , Feb 10, 2014

BDC Program with OOABAP


By CD Raju , Oct 11, 2013

Case Study On HCM Outbound Interfaces


By Former Member , Jan 28, 2014

Related Questions

Interfaces and inheritance?


By Former Member , Nov 05, 2007
example on interfaces
By Former Member , Dec 12, 2006
https://blogs.sap.com/2013/06/19/example-on-interfacesooabap/ 4/9
2/27/2020 Example on Interfaces(OOABAP) | SAP Blogs

Regarding OOABAP
By Former Member , Aug 13, 2007

3 Comments

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

Naimesh Patel

June 19, 2013 at 3:55 pm

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.

https://blogs.sap.com/2013/06/19/example-on-interfacesooabap/ 5/9
2/27/2020 Example on Interfaces(OOABAP) | SAP Blogs

class lcl_square DEFINITION.

   PUBLIC SECTION.

     INTERFACEs: lif_shape.

ENDCLASS.

START–OF–SELECTION.

   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 de ning aliases, you can de ne the visibility of the component
You can use Interfaces to achieve the Multiple Inheritance

https://blogs.sap.com/2013/06/19/example-on-interfacesooabap/ 6/9
2/27/2020 Example on Interfaces(OOABAP) | SAP Blogs

Regards,
Naimesh Patel

Like (0)

Former Member | Post author

June 19, 2013 at 4:10 pm

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

Like (0)

akira jain

November 12, 2016 at 3:09 am

Example:

Report ZINTERFACE1.
INTERFACE my_interface1.
Methods msg.
ENDINTERFACE.

CLASS num_counter De nition.


PUBLIC Section.
INTERFACES my_interface1.
Methods add_number.
PRIVATE Section.

https://blogs.sap.com/2013/06/19/example-on-interfacesooabap/ 7/9
2/27/2020 Example on Interfaces(OOABAP) | SAP Blogs

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 De nition.


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

https://blogs.sap.com/2013/06/19/example-on-interfacesooabap/ 8/9
2/27/2020 Example on Interfaces(OOABAP) | SAP Blogs

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

More Infomations: http://sapabapcentral.blogspot.in/p/142-sap-abap-interfaces.html

Like (0)

Find us on

Privacy Terms of Use

Legal Disclosure Copyright

Trademark Cookie Preferences

Newsletter Support

https://blogs.sap.com/2013/06/19/example-on-interfacesooabap/ 9/9

You might also like