Professional Documents
Culture Documents
Community
Former Member
June 19, 2013 1 minute read
Example on Interfaces(OOABAP)
Follow RSS feed Like
Hi all,
In this document i want to explain the general and simple example on Interfaces concept of OOABAP.
Interfaces are pure abstract classes. i.e, by default all the methods of interface are abstract.
Creation of object is not possible since they are not implemented completely.
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.
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.
INTERFACE Rectangle.
“Components declaration”
ENDINTERFACE.
INTERFACE Square.
“components declaration”
ENDINTERFACE.
“Class Definition
This lcl_interface class declares the two interfaces square and rectangle.
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.
method rectangle~area.
write: / res.
endmethod.
method rectangle~perimeter.
write: / res.
ENDMETHOD.
METHOD square~area.
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
ob->rectangle~perimeter,
ob->square~area,
ob->square~perimeter.
Alert Moderator
Assigned tags
ABAP Development |
FRIENDSHIP in OOABAP
By Pramod Repaka , Feb 10, 2014
Related Questions
Regarding OOABAP
By Former Member , Aug 13, 2007
3 Comments
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:
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
PUBLIC SECTION.
ENDCLASS.
START–OF–SELECTION.
lo_shape->area( ).
method lif_shape~area.
ENDMETHOD.
method lif_shape~perimeter.
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:
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)
Hi Naimesh,
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…
Thank You
Venkat
Like (0)
akira jain
Example:
Report ZINTERFACE1.
INTERFACE my_interface1.
Methods msg.
ENDINTERFACE.
https://blogs.sap.com/2013/06/19/example-on-interfacesooabap/ 7/9
2/27/2020 Example on Interfaces(OOABAP) | SAP Blogs
Method add_number.
ADD 7 TO num.
EndMethod.
ENDCLASS.
Method speed1.
Add 4 To wheel1.
EndMethod.
ENDCLASS.
Start-Of-Selection.
Data object1 Type Ref To num_counter.
Create Object object1.
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.
Like (0)
Find us on
Newsletter Support
https://blogs.sap.com/2013/06/19/example-on-interfacesooabap/ 9/9