Polymorphism using inheritance in ABAP Objects

Go down

Polymorphism using inheritance in ABAP Objects

Post  Uma_ABAP on Sun Apr 01, 2012 1:30 pm


With inheritance, a reference variable defined with respect to a class c1 may not only point to instances of c1 but also to instances of subclasses of c1. You can even create subclass objects using a reference variable typed with respect to a superclass. A reference variable is said to have static and a dynamic type.

Using inheritance only, the static type is either the same as the dynamic type or is a
superclass of the dynamic type. In other words, we can use the reference of super class to point to the instance of the sub class, and using this reference we can access the components of the subclass. A point of note, we can access only those components of the subclass that it has inherited from the super class. For example suppose there are two class father and child. And suppose father is the super class and the child is the subclass .

Now we create two instances say one of father and one of child ,o_father and o_child respectively.

From the point of view of polymorphism we need to have a common point of access of the components of o_father as well as of o_child (that are common to both ). Then how do we create the common point ??

Well we con do that by a reference variable, whose static part is of type father and dynamic part should point to the child. Let’s take an example.

Code:

CLASS father DEFINITION.
PUBLIC SECTION.
METHODS: f_meth.
ENDCLASS.

CLASS father IMPLEMENTATION.
METHOD f_meth.
Write: / ‘father method’.
ENDMETHOD.
ENDCLASS.

CLASS child DEFINITION INHERITING FROM FATHER.
PUBLIC SECTION.
METHODS : f_meth REDEFINITION,
C_meth.
ENDCLASS.

CLASS child IMPLEMENTATION.
METHOD f_meth.
Write: ‘father method redefined’.
ENDMETHOD.
METHOD c_meth.
Write: ‘child method’.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

DATA: f_ref TYPE REF TO father,
Ref TYPE REF TO father,
C_ref TYPE REF TO child.
CREATE OBJECT: C_ref,f_ref.
Ref = f_ref.
Call method ref->f_meth.
Ref = C_ref.
Call method ref->f_meth.
** call method ref->c_meth . “this assignment is not possible.
*************** out put ***********************
father method
father method redefined.

Code:

In this example the previous example has been shown with interface.
interface my_ic.
methods:add_value.
endinterface.
class father definition.
public section.
interfaces my_ic.
endclass.

class father implementation.
method my_ic~add_value .
write:/ 'result father' .
endmethod.
endclass.

class child definition inheriting from father.
public section.
methods:my_ic~add_value redefinition,
child_meth.
endclass.

class child implementation.
method my_ic~add_value.
write:/ 'child'.
endmethod.
method child_meth.
write:/ 'child own method'.
endmethod.
endclass.

start-of-selection.

data:c_ref type ref to child,
f_ref type ref to father,
i_ref type ref to my_ic.
create object: f_ref type child.
call method f_ref->my_ic~add_value.
*call method f_ref->child_meth. " this is not possible as this was not
"inherited

Uma_ABAP

Posts : 56
Join date : 2012-03-31

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum