Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Interfaces and inheritance?

former_member2382
Active Participant
0 Kudos

Dear All,

Can you tell me what are interfaces and inheritance in OOABAP and how do implement them? Its better if you could provide an example for the same.

Thanks,

Parvez.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Parvez,

Go to the below link to know about interfaces.

<a href="http://help.sap.com/saphelp_nw70/helpdata/en/c3/225b6254f411d194a60000e8353423/frameset.htm">Interfaces</a>

Actually if you go there it provides you all information about the OO abap. And if you want to have code samples Go to ABAPDOCU transaction and expand the tree ABAP OBJECTS. you will find prgrams on all concepts of oo abap like inheritance, interfaces and polymorphism.

If you have time i suggest you to read this link starting from the basics.

Reward points if useful,

Aleem.

3 REPLIES 3

Former Member
0 Kudos

Hi

Inheritence should be used when dealing with related objects of the same category...for e.g. it makes sense for a class of 'Cars' to inherit from a class named 'Vehicle' becuase a car is a type of vehicle. As the word 'inherit' mean"to receive as if by succession from predecessors", so when you make a class inherit its parent class (or its SuperClass) it receives, in this case, all the properties (Methods/Events) and Attributes (Public and Protected data, constants etc.) of its superclass and the class itself is then known as a SubClass...so in short, Class "Vehicle" can be a superclass of the Class "Car" if car inherits vehicle (makes sense to do so).

See example of inheritance below from the book "ABAP Objects" by Horst Keller. I recommend you create this program in your system and run it in debugging mode to understand the concept of inheritance. Note - class "object" is the parent or root Class of all classes, that is to say all classes automatically inherit 'object' class therefore the statement

CLASS vehicle DEFINITION INHERITING FROM object.

does not have much significance in other contexts but its explicitly defined here for demonstration purpose - when you copy the code in your system you could instead say

CLASS vehicle DEFINITION.

and I believe it will work exactly the same.

REPORT s_simple_inheritance.

  • Class Declarations

CLASS vehicle DEFINITION INHERITING FROM object.

PUBLIC SECTION.

METHODS: accelerate,

write_status.

PROTECTED SECTION.

DATA speed TYPE i.

ENDCLASS.

CLASS plane DEFINITION INHERITING FROM vehicle.

PUBLIC SECTION.

METHODS: rise.

PROTECTED SECTION.

DATA altitude TYPE i.

ENDCLASS.

CLASS ship DEFINITION INHERITING FROM vehicle.

ENDCLASS.

  • Class Implementations

CLASS vehicle IMPLEMENTATION.

METHOD accelerate.

speed = speed + 1.

ENDMETHOD.

METHOD write_status.

WRITE: / 'Speed:', speed.

ENDMETHOD.

ENDCLASS.

CLASS plane IMPLEMENTATION.

METHOD rise.

altitude = altitude + 1.

ENDMETHOD.

ENDCLASS.

  • Global Data

DATA: plane_ref TYPE REF TO plane,

ship_ref TYPE REF TO ship.

  • Classical Processing Blocks

START-OF-SELECTION.

CREATE OBJECT: plane_ref,

ship_ref.

CALL METHOD: plane_ref->accelerate,

plane_ref->rise,

plane_ref->write_status,

ship_ref->accelerate,

ship_ref->write_status.

Have a look at below links which gives you details about ABAP interfaces:

http://help.sap.com/saphelp_nw2004s/helpdata/en/c9/5472fc787f11d194c90000e8353423/frameset.htm

http://help.sap.com/saphelp_46c/helpdata/en/22/042427488911d189490000e829fbbd/frameset.htm

<b>Reward iof usefull</b>

Former Member
0 Kudos

Hi Parvez,

Go to the below link to know about interfaces.

<a href="http://help.sap.com/saphelp_nw70/helpdata/en/c3/225b6254f411d194a60000e8353423/frameset.htm">Interfaces</a>

Actually if you go there it provides you all information about the OO abap. And if you want to have code samples Go to ABAPDOCU transaction and expand the tree ABAP OBJECTS. you will find prgrams on all concepts of oo abap like inheritance, interfaces and polymorphism.

If you have time i suggest you to read this link starting from the basics.

Reward points if useful,

Aleem.

Shruthi29
Explorer
0 Kudos

Hi,

In inheritance the child class will acquire/inherit the properties of parent class.

example: There are two classes class A and class B , class A is the super/parent class and class B is the child class. As we know child will inherit properties/behavior from their parents in the same way, in the child class B will inherit the properties from parent class A.

Interface is used when two similar classes have a method with the same name , but the functionalities are different from each other but there will be no implementation in the interfaces.