Polymorphism using inheritance in ABAP Objects
Page 1 of 1
Polymorphism using inheritance in ABAP Objects
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
Similar topics
» Concept of Polymorphism in ABAP Objects
» Concept of Polymorphism in ABAP Objects
» Polymorphism using interface in ABAP Objects
» Creating multiple Objects in ABAP Objects
» Difference between Procedural ABAP and ABAP Objects
» Concept of Polymorphism in ABAP Objects
» Polymorphism using interface in ABAP Objects
» Creating multiple Objects in ABAP Objects
» Difference between Procedural ABAP and ABAP Objects
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Mon Aug 20, 2012 4:37 pm by Admin
» Difference between Type and Like
Thu Jun 07, 2012 2:40 pm by Admin
» How to handle table control fields in Module Pool program?
Wed May 09, 2012 2:41 am by govind
» Three tables inner join in SAP
Thu May 03, 2012 4:47 pm by Uma_ABAP
» Creating table type in ABAP
Sat Apr 28, 2012 9:03 pm by Uma_ABAP
» Creating user defined types and structures in ABAP
Sat Apr 28, 2012 8:55 pm by Uma_ABAP
» Creating Dynamic Internal Table and Dynamic Field catalog in SAP-ABAP
Fri Apr 27, 2012 11:00 am by Admin
» Copy Custom PF-Status from Standard program to Custom Program in ALV
Thu Apr 26, 2012 9:52 pm by Uma_ABAP
» Calculate/find number of working days between two dates in SAP
Tue Apr 24, 2012 2:06 pm by Uma_ABAP