Polymorphism using interface in ABAP Objects
Page 1 of 1
Polymorphism using interface in ABAP Objects
Interfaces allow you to use different classes in a uniform way using interface references .For example, interfaces that are implemented in different classes extend the public scope of each class by the same set of components.
If a class does not have any class-specific public components, the interfaces define the entire public face of the class. As we know that any no of class can implement the interface differently ,and we have seen that how we can access the components of the interface through the class objects.
But we want to have a common point of access for the different implementation of the interface methods. So how do we do that?
Well we can do that by using an interface reference; we can access the different class objects by pointing the interface reference to that class.
- Code:
Let’s take an example:
INTERFACE my_infc.
METHODS: I_method.
ENDINTERFACE.
CLASS c1 DEFINITION.
PUBLIC SECTION.
INTERFACES: my_infc.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD my_infc~I_method.
Write:/ ‘method for c1’.
ENDMETHOD.
ENDCLASS.
CLASS c2 DEFINITION.
PUBLIC SECTION.
INTERFACES: my_infc.
ENDCLASS.
CLASS c2 IMPLEMENTATION.
METHOD my_infc~I_method.
Write:/ ‘method for c2’.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: iref type ref to my_infc,
C1ref type ref to c1.
C2ref type ref to c2.
CREATE OBJECT: C1ref, C2ref .
Iref = C1ref. “reference of class c1 is passed to interface reference
Iref->I_method. “method implemented in class c1 is called
Iref = C2ref. “reference of class c1 is passed to interface reference
Iref->I_method. “method implemented in class c2 is called.
********************* OUTPUT**************
method for c1
method for c2
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 inheritance in ABAP Objects
» Creating Interface in ABAP Objects with example
» Introduction to interface in ABAP objects
» Concept of Polymorphism in ABAP Objects
» Polymorphism using inheritance in ABAP Objects
» Creating Interface in ABAP Objects with example
» Introduction to interface in 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