‎2007 Feb 14 6:29 PM
Hi All,
Is it possible to change interface parameter defined in the super class in the method of inherited class?
Regards,
Akshay
‎2007 Feb 15 1:56 PM
Hello Akshay,
Let me understand your query.
There is a superclass which implements an interface. You have defined interface attribute or method in one way in the superclass.
Now you have a sub class inheriting from the superclass. You want to change the
interface attribute value or method implementtaion in the subclass.
This is possible and is one type of POLYMORPHISM. Please see the following
program.
I have redefined in the subclass, 2 methods of interface, implemented in the superclass.
&----
*& Interface (Definiton) zl_lif_vehicle
&----
Definition of Vehicle Interface. *
This interface will be used for superclass vehicle and *
subclass bike *
----
INTERFACE zl_lif_vehicle.
METHODS: set_make IMPORTING im_make TYPE string
im_model TYPE string,
get_make EXPORTING ex_make TYPE string
ex_model TYPE string.
ENDINTERFACE. "zl_lif_vehicle
&----
*& Class (Definiton) zl_lcl_vehicle
&----
Definition of Vehicle class
----
CLASS zl_lcl_vehicle DEFINITION.
PUBLIC SECTION.
INTERFACES zl_lif_vehicle.
PROTECTED SECTION.
DATA: gv_make TYPE string, " Vehicle make
gv_model TYPE string. " Type or model
ENDCLASS. "zl_lcl_vehicle DEFINITION
&----
*& Class (Definiton) zl_lcl_bike
&----
Definition of Bike class
----
CLASS zl_lcl_bike DEFINITION INHERITING FROM zl_lcl_vehicle.
PUBLIC SECTION.
Subclass need not implement the interface, already implemented
in the superclass.
Since methods are already implemented in superclass, to chnage
implementation in subclass, use REDEFINITION addition.
METHODS: zl_lif_vehicle~set_make REDEFINITION,
zl_lif_vehicle~get_make REDEFINITION.
PRIVATE SECTION.
DATA gv_veh_type TYPE string.
ENDCLASS. "zl_lcl_bike DEFINITION
&----
*& Class (Implementation) zl_lcl_car
&----
Implementation of Car class
----
CLASS zl_lcl_vehicle IMPLEMENTATION.
*Implementing the interface components in the class. The interface
*components are distinguished from other components in implementing
*class by prefixing interface name followed by a tilde ~
METHOD zl_lif_vehicle~set_make.
IF im_make IS NOT INITIAL
AND im_model IS NOT INITIAL.
gv_make = im_make.
gv_model = im_model.
ENDIF.
ENDMETHOD. "set_make
METHOD zl_lif_vehicle~get_make.
ex_make = gv_make.
ex_model = gv_model.
ENDMETHOD. "get_make
ENDCLASS. "zl_lcl_vehicle IMPLEMENTATION
&----
*& Class (Implementation) zl_lcl_bike
&----
Implementation of Bike class
----
CLASS zl_lcl_bike IMPLEMENTATION.
METHOD zl_lif_vehicle~set_make.
Implementation of this method in the bike class is different from
that in superclass VEHICLE. Thus any method defined in the interface
can be implemented differently in different classes.
IF im_make IS NOT INITIAL
AND im_model IS NOT INITIAL.
gv_veh_type = 'bike'.
CONCATENATE gv_veh_type '#' im_make INTO gv_make.
gv_model = im_model.
ENDIF.
ENDMETHOD. "set_make
METHOD zl_lif_vehicle~get_make.
Implementation changed from that in superclass
CONCATENATE gv_make '#' INTO ex_make.
ex_model = gv_model.
ENDMETHOD. "get_make
ENDCLASS. "zl_lcl_bike IMPLEMENTATION
*Declaring reference variable with reference to the car & bike class and
a reference variable typed to the interface
DATA: z_vehicle TYPE REF TO zl_lcl_vehicle,
z_bike TYPE REF TO zl_lcl_bike,
z_partner TYPE REF TO zl_lif_vehicle.
DATA : gv_make TYPE string,
gv_model TYPE string.
START-OF-SELECTION.
Create instance of the class
CREATE OBJECT: z_vehicle,
z_bike.
*&----
To perfrom polymorphism with interfaces copy the object reference
of the superclass to a reference variable typed to the interface.
z_partner = z_vehicle.
The instance of the implementing class can now be addressed
using the interface.
z_partner->set_make( EXPORTING im_make = 'HYUNDAI'
im_model = 'SANTRO' ).
z_partner->get_make( IMPORTING ex_make = gv_make
ex_model = gv_model ).
WRITE: / 'Make :', gv_make,
/ 'Model :', gv_model.
*&----
Object reference variable of the sub class copied to the
Interface reference variable
z_partner = z_bike.
Implementation that is executed when methods are called depends on the
object to which interface reference variable currently refers (bike in
this case).
z_partner->set_make( EXPORTING im_make = 'SUZUKI'
im_model = 'HAYABUSA' ).
z_partner->get_make( IMPORTING ex_make = gv_make
ex_model = gv_model ).
WRITE: / 'Make :', gv_make,
/ 'Model :', gv_model.
Award points if found useful.
Regards
Indrajit.
‎2007 Feb 14 6:35 PM
Hi,
You can not change the interface parameters . but you can change the values of the parameters because the values will be avialble in the subclass and change the value
Regards
Sudheer
‎2007 Feb 14 8:07 PM
Hello Akshay
With the new <b>enhancement framework</b> (availabe since SAP release 7.00) you may be able to enrich the interface of inherited classes with additional parameters yet you cannot change the inherited ones from the superclass.
Regards
Uwe
‎2007 Feb 15 1:56 PM
Hello Akshay,
Let me understand your query.
There is a superclass which implements an interface. You have defined interface attribute or method in one way in the superclass.
Now you have a sub class inheriting from the superclass. You want to change the
interface attribute value or method implementtaion in the subclass.
This is possible and is one type of POLYMORPHISM. Please see the following
program.
I have redefined in the subclass, 2 methods of interface, implemented in the superclass.
&----
*& Interface (Definiton) zl_lif_vehicle
&----
Definition of Vehicle Interface. *
This interface will be used for superclass vehicle and *
subclass bike *
----
INTERFACE zl_lif_vehicle.
METHODS: set_make IMPORTING im_make TYPE string
im_model TYPE string,
get_make EXPORTING ex_make TYPE string
ex_model TYPE string.
ENDINTERFACE. "zl_lif_vehicle
&----
*& Class (Definiton) zl_lcl_vehicle
&----
Definition of Vehicle class
----
CLASS zl_lcl_vehicle DEFINITION.
PUBLIC SECTION.
INTERFACES zl_lif_vehicle.
PROTECTED SECTION.
DATA: gv_make TYPE string, " Vehicle make
gv_model TYPE string. " Type or model
ENDCLASS. "zl_lcl_vehicle DEFINITION
&----
*& Class (Definiton) zl_lcl_bike
&----
Definition of Bike class
----
CLASS zl_lcl_bike DEFINITION INHERITING FROM zl_lcl_vehicle.
PUBLIC SECTION.
Subclass need not implement the interface, already implemented
in the superclass.
Since methods are already implemented in superclass, to chnage
implementation in subclass, use REDEFINITION addition.
METHODS: zl_lif_vehicle~set_make REDEFINITION,
zl_lif_vehicle~get_make REDEFINITION.
PRIVATE SECTION.
DATA gv_veh_type TYPE string.
ENDCLASS. "zl_lcl_bike DEFINITION
&----
*& Class (Implementation) zl_lcl_car
&----
Implementation of Car class
----
CLASS zl_lcl_vehicle IMPLEMENTATION.
*Implementing the interface components in the class. The interface
*components are distinguished from other components in implementing
*class by prefixing interface name followed by a tilde ~
METHOD zl_lif_vehicle~set_make.
IF im_make IS NOT INITIAL
AND im_model IS NOT INITIAL.
gv_make = im_make.
gv_model = im_model.
ENDIF.
ENDMETHOD. "set_make
METHOD zl_lif_vehicle~get_make.
ex_make = gv_make.
ex_model = gv_model.
ENDMETHOD. "get_make
ENDCLASS. "zl_lcl_vehicle IMPLEMENTATION
&----
*& Class (Implementation) zl_lcl_bike
&----
Implementation of Bike class
----
CLASS zl_lcl_bike IMPLEMENTATION.
METHOD zl_lif_vehicle~set_make.
Implementation of this method in the bike class is different from
that in superclass VEHICLE. Thus any method defined in the interface
can be implemented differently in different classes.
IF im_make IS NOT INITIAL
AND im_model IS NOT INITIAL.
gv_veh_type = 'bike'.
CONCATENATE gv_veh_type '#' im_make INTO gv_make.
gv_model = im_model.
ENDIF.
ENDMETHOD. "set_make
METHOD zl_lif_vehicle~get_make.
Implementation changed from that in superclass
CONCATENATE gv_make '#' INTO ex_make.
ex_model = gv_model.
ENDMETHOD. "get_make
ENDCLASS. "zl_lcl_bike IMPLEMENTATION
*Declaring reference variable with reference to the car & bike class and
a reference variable typed to the interface
DATA: z_vehicle TYPE REF TO zl_lcl_vehicle,
z_bike TYPE REF TO zl_lcl_bike,
z_partner TYPE REF TO zl_lif_vehicle.
DATA : gv_make TYPE string,
gv_model TYPE string.
START-OF-SELECTION.
Create instance of the class
CREATE OBJECT: z_vehicle,
z_bike.
*&----
To perfrom polymorphism with interfaces copy the object reference
of the superclass to a reference variable typed to the interface.
z_partner = z_vehicle.
The instance of the implementing class can now be addressed
using the interface.
z_partner->set_make( EXPORTING im_make = 'HYUNDAI'
im_model = 'SANTRO' ).
z_partner->get_make( IMPORTING ex_make = gv_make
ex_model = gv_model ).
WRITE: / 'Make :', gv_make,
/ 'Model :', gv_model.
*&----
Object reference variable of the sub class copied to the
Interface reference variable
z_partner = z_bike.
Implementation that is executed when methods are called depends on the
object to which interface reference variable currently refers (bike in
this case).
z_partner->set_make( EXPORTING im_make = 'SUZUKI'
im_model = 'HAYABUSA' ).
z_partner->get_make( IMPORTING ex_make = gv_make
ex_model = gv_model ).
WRITE: / 'Make :', gv_make,
/ 'Model :', gv_model.
Award points if found useful.
Regards
Indrajit.
‎2007 Feb 15 4:32 PM
Yes, It is possible to change the interface parameter(Value only) in subclasses.
‎2007 Feb 25 1:10 PM
Hello Akshay,
Something which really surprised and confused me was your rationale of assigning points to the responses to your post.
None of us are here for points, knowledge sharing is the priority, still we have to keep in mind that we should assign points carefully.
Regards
Indrajit.