‎2007 Jul 03 9:15 AM
Is there any phenomenon by name method overriding in abap like java.If it was there can any body explain it in detail,or if it was not there ,then is there any other phenomenon which have a similar functionality like method overriding
‎2007 Jul 03 10:30 AM
Hi check this out it may be helpful.
) In overloading, there is a relationship between methods available in the same class whereas in overriding, there is relationship between a superclass method and subclass method.
b) Overloading does not block inheritance from the superclass whereas overriding blocks inheritance from the superclass.
c) In overloading, separate methods share the same name whereas in overriding, subclass method replaces the superclass.
d) Overloading must have different method signatures whereas overriding must have same signature.
CLASS zl_lcl_vehicle DEFINITION.
PUBLIC SECTION.
Signature of method
METHODS: set_make
IMPORTING value(im_make) TYPE string " Pass by value
im_model TYPE string," Pass by reference
ENDCLASS. "zl_lcl_vehicle DEFINITION
CLASS zl_lcl_vehicle IMPLEMENTATION.
Implementation of method.
METHOD 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
ENDCLASS. "zl_lcl_vehicle
Overloading means changing signature as well as implementation of a method.
Overriding is changing only implementation of method with signature unchanged.
From ABAP perspective, only the CONSTRUCTOR method can be overloaded in a subclass i.e both the signature and implementation can be adapted in subclass.
Any other method can't be overloaded. It can only be redefined/overridden i.e implementation changed with signature unchanged.
Award points if found useful.
Regards,
Rajesh
‎2007 Jul 03 11:43 AM
‎2007 Jul 04 8:39 AM
Hi Sandeep,
Overriding is changing method implementation with signature unchanged.
Overloading is changing signature as well as implementation.
In ABAP, overlaoding is not possible for methods with one exception. Only the constructor method can be overlaoded in a subclass during inheritance.
For all other methods, only overriding is possible.
Check out the following programs.
<u><b>Method overriding</b></u>
&----
*& Class (Definiton) zl_lcl_vehicle
&----
Definition of the Superclass
----
CLASS zl_lcl_vehicle DEFINITION.
PUBLIC SECTION.
Visible to all
METHODS: set_make IMPORTING im_make TYPE string
im_model TYPE string,
get_make EXPORTING ex_make TYPE string
ex_model TYPE string.
PROTECTED SECTION.
Visible within class and all subclasses
DATA gv_n_o_veh TYPE i.
PRIVATE SECTION.
Only visible within the class
DATA: gv_make TYPE string, " Vehicle make
gv_model TYPE string. " Type or model
ENDCLASS. "zl_lcl_vehicle DEFINITION
&----
*& Class (Implementation) zl_lcl_vehicle
&----
Implementation of the Superclass
----
CLASS zl_lcl_vehicle IMPLEMENTATION.
METHOD 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 get_make.
ex_make = gv_make.
ex_model = gv_model.
ENDMETHOD. "get_make
ENDCLASS. "zl_lcl_vehicle
&----
*& Class (Definiton) zl_lcl_car
&----
Definition of the Subclass
----
CLASS zl_lcl_car DEFINITION INHERITING FROM zl_lcl_vehicle.
PUBLIC SECTION.
METHODS: set_vehtype IMPORTING im_vehtype TYPE string
im_n_o_veh TYPE i,
get_vehtype EXPORTING ex_vehtype TYPE string
ex_n_o_veh TYPE i,
Begin of IC240107
To change the implementation of a superclass instance method in *
subclass, without changing the method signature, the method must be *
defined in subclass with REDIFINITION addition. There is no need to *
define the method parameters and exceptions again. *
Redifinition is not possible for private methods. *
Redefining methods introduces POLYMORPHISM in classes. *
REDEFINITION addition must be used to implement an abstract method *
of a superclass in a subclass. However a final method in a *
superclass can't be redefined in superclass. *
The constructors of a class are always final and cannot be redefined.*
get_make REDEFINITION.
End of IC240107
PRIVATE SECTION.
DATA gv_vehtype TYPE string.
ENDCLASS. "zl_lcl_car DEFINITION
&----
*& Class (Implementation) zl_lcl_car
&----
Implementation of the Subclass
----
CLASS zl_lcl_car IMPLEMENTATION.
METHOD set_vehtype.
IF im_vehtype IS NOT INITIAL.
gv_vehtype = im_vehtype.
Protected component of superclass can be directly accessed in subclass. *
The same applies for public components, private compenets of superclass *
have to be accessed using public or protected methods of superclass. *
gv_n_o_veh = im_n_o_veh.
ENDIF.
ENDMETHOD. "set_vehtype
METHOD get_vehtype.
ex_vehtype = gv_vehtype.
ex_n_o_veh = gv_n_o_veh.
ENDMETHOD. "get_vehtype
Begin of IC240107
When REDIFINITION addition is used with any method in subclass *
definition a new implementation part must be specified for the *
inherited method. *
METHOD get_make.
To access the superclass method from the redefined method, use the *
pseudo-reference SUPER *
super->get_make( IMPORTING ex_make = ex_make
ex_model = ex_model ).
ENDMETHOD. "get_make
End of IC240107
ENDCLASS. "zl_lcl_car
Declaring a reference variable with reference to the subclass.
DATA: z_car TYPE REF TO zl_lcl_car.
DATA: gv_make TYPE string,
gv_model TYPE string,
gv_vehtype TYPE string,
gv_n_o_veh TYPE i.
START-OF-SELECTION.
Creating an instance of the subclass
CREATE OBJECT: z_car.
Accessing the public methods of superclass using subclass reference
z_car->set_make( EXPORTING im_make = 'MARUTI'
im_model = '800' ).
POLYMORPHISM - There are 2 implementations of the method get_make *
one in superclass and one in subclass. Since the method is *
redefined in subclass, subclass implementation will be executed *
if subclass reference is used. *
z_car->get_make( IMPORTING ex_make = gv_make
ex_model = gv_model ).
Accessing the methods of subclass using subclass reference
z_car->set_vehtype( EXPORTING im_vehtype = 'Car'
im_n_o_veh = '10' ).
z_car->get_vehtype( IMPORTING ex_vehtype = gv_vehtype
ex_n_o_veh = gv_n_o_veh ).
WRITE: / 'Vehicle type :', gv_vehtype,
/ 'Make :', gv_make,
/ 'Model :', gv_model,
/ 'No of ordered vehicles :', gv_n_o_veh.
<u><b>Constructor Overlaoding</b></u>
&----
*& Class (Definiton) zl_lcl_vehicle
&----
Definition of the Superclass
----
CLASS zl_lcl_vehicle DEFINITION.
PUBLIC SECTION.
Visible to all
METHODS: set_make IMPORTING im_make TYPE string
im_model TYPE string,
get_make EXPORTING ex_make TYPE string
ex_model TYPE string,
Begin of IC240107
Superclass Constructor
constructor IMPORTING im_n_o_vehicle TYPE i.
End of IC240107
PROTECTED SECTION.
Visible within class and all subclasses
DATA gv_n_o_veh TYPE i.
PRIVATE SECTION.
Only visible within the class
DATA: gv_make TYPE string, " Vehicle make
gv_model TYPE string. " Type or model
ENDCLASS. "zl_lcl_vehicle DEFINITION
&----
*& Class (Definiton) zl_lcl_vehicle
&----
Definition of the Subclass
----
CLASS zl_lcl_car DEFINITION INHERITING FROM zl_lcl_vehicle.
PUBLIC SECTION.
METHODS: get_vehtype EXPORTING ex_vehtype TYPE string
ex_n_o_veh TYPE i,
Begin of IC240107
Constructor Overloading. Both the signature and implementation of *
method can be adapted in the subclass. *
constructor IMPORTING ims_n_o_vehicle TYPE i
ims_vehtype TYPE string.
End of IC240107
PRIVATE SECTION.
DATA gv_vehtype TYPE string.
ENDCLASS. "zl_lcl_car DEFINITION
&----
*& Class (Implementation) zl_lcl_vehicle
&----
Implementation of the Superclass
----
CLASS zl_lcl_vehicle IMPLEMENTATION.
METHOD 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 get_make.
ex_make = gv_make.
ex_model = gv_model.
ENDMETHOD. "get_make
Begin of IC240107
Superclass constructor implementation
METHOD constructor.
gv_n_o_veh = im_n_o_vehicle.
ENDMETHOD. "constructor
End of IC240107
ENDCLASS. "zl_lcl_vehicle
&----
*& Class (Implementation) zl_lcl_car
&----
Implementation of the Subclass
----
CLASS zl_lcl_car IMPLEMENTATION.
METHOD get_vehtype.
ex_vehtype = gv_vehtype.
ex_n_o_veh = gv_n_o_veh.
ENDMETHOD. "get_vehtype
Begin of IC240107
Subclass constructor implementation
METHOD constructor.
If subclass constructor is overloaded, it is mandatory to call
superclass constructor within subclass constructor, because superclass
constructor is meant to be executed when object is created for super
or sub class. If subclass constructor is not overloaded, then the
runtime system will ensure this automatically.
Static constructors in superclass are always executed automatically
before any static constructor in subclass is executed.
Comment out the following and see what happens
super->constructor( EXPORTING im_n_o_vehicle = ims_n_o_vehicle ).
gv_vehtype = ims_vehtype.
ENDMETHOD. "constructor
End of IC240107
ENDCLASS. "zl_lcl_car
Declaring a reference variable with reference to the subclass.
DATA z_car TYPE REF TO zl_lcl_car.
DATA: gv_make TYPE string,
gv_model TYPE string,
gv_vehtype TYPE string,
gv_n_o_veh TYPE i.
START-OF-SELECTION.
Creating an instance of the subclass
CREATE OBJECT z_car
Begin of IC240107
EXPORTING ims_vehtype = 'Car'
ims_n_o_vehicle = '10' .
End of IC240107
Accessing the public methods of superclass
z_car->set_make( EXPORTING im_make = 'MARUTI'
im_model = '800' ).
z_car->get_make( IMPORTING ex_make = gv_make
ex_model = gv_model ).
z_car->get_vehtype( IMPORTING ex_vehtype = gv_vehtype
ex_n_o_veh = gv_n_o_veh ).
WRITE: / 'Vehicle type :', gv_vehtype,
/ 'Make :', gv_make,
/ 'Model :', gv_model,
/ 'No of ordered vehicles :', gv_n_o_veh.
<u><b>
Award points if found useful.</b></u>
Regards
Indrajit