‎2007 Jul 05 7:51 AM
what is the significance of final keyword in abap,How can we avoid a class being inherited by another class
‎2007 Jul 05 9:02 AM
hi,
final keyword denotes that it is the final implementation for that method or variable or class. You cant override that method/variable/class any more.
and using this final keyword we can avoid a class being inherited by other..
these are the concepts related to java..in J2e versions it will support..
thanks
jaideep
if useful reward points
‎2007 Jul 05 9:02 AM
hi,
final keyword denotes that it is the final implementation for that method or variable or class. You cant override that method/variable/class any more.
and using this final keyword we can avoid a class being inherited by other..
these are the concepts related to java..in J2e versions it will support..
thanks
jaideep
if useful reward points
‎2007 Jul 05 9:22 AM
Hi Sandeep,
IN ABAP, the key word FINAL can be used with class definition and method definition.
1. If we define a class as final , we cannot INHERIT from this calss.
Class CLASS1 DEFINITION FINAL INHERITING FROM SUPER_CLASS.
This is possible.
But
Class CLASS2 DEFINITION FINAL INHERITING FROM CLASS1.
THis is not possible as we have defined CLASS1 as final class.
2. If we define a method as final, it cannot be REDEFINED in sub classwes.
Eg.
CLASS CLASS1 DEFINITON
PUBLIC SECTION.
METHODS: METHOD1 FINAL.
ENDCLASS.
Then,
CLASS CLASS2 DEFINITION INHERITING FROM CLASS1.
PUBLIC SECTION.
METHODS: METHOD1 REDIFINITION.
ENDCLASS.
This is not possible as we have defined METHOD1 as FINAL in CLASS1.
Please reward with points if it is useful.
‎2007 Jul 06 6:41 AM
Hi Sundeep,
If this answer was helpful , please reward with points.
‎2007 Jul 06 8:13 AM
Hi Sandeep,
If we add "FINAL" addition to a class, no other class no subclasses can be inherited. It marks the last node of Inheritance tree. All methods in a Final class are automatically Final.
In a class which is not final, a method can be defined as FINAL. Such a
method cannot be redefined in a subclass.
Please see the example program.
&----
*& Class (Definiton) zl_lcl_vehicle
&----
Definition of Superclass *
----
CLASS zl_lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: set_make IMPORTING im_make TYPE string
im_model TYPE string,
get_make EXPORTING ex_make TYPE string
ex_model TYPE string,
Final Methods cannot be redefined in subclasses. Final methods *
cannot also be abstract. The constructors of the class are always *
final implicitly. *
get_n_o_veh_type FINAL EXPORTING ex_veh_typ TYPE i.
PRIVATE SECTION.
DATA: gv_make TYPE string, " Vehicle make
gv_model TYPE string. " Type or model
CLASS-DATA gv_veh_type_cnt TYPE i.
ENDCLASS. "zl_lcl_vehicle DEFINITION
&----
*& Class (Implementation) zl_lcl_vehicle
&----
Implementation of 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
This method is used to count the number of vehicle types. *
This number is later used to asign a vehicle code for a vehicle type *
METHOD get_n_o_veh_type.
gv_veh_type_cnt = gv_veh_type_cnt + 1.
ex_veh_typ = gv_veh_type_cnt.
ENDMETHOD. "get_n_o_veh_type
ENDCLASS. "zl_lcl_vehicle
&----
*& Class (Definiton) zl_lcl_car
&----
Definition of Subclass *
This class is defined as FINAL which means it can't be *
inherited. Final classes are the last branch in an *
inheritance tree. In a final classes, the declaration of *
protected components is synonymous with the declaration of *
private components. *
----
CLASS zl_lcl_car DEFINITION FINAL
INHERITING FROM zl_lcl_vehicle.
PUBLIC SECTION.
METHODS: set_make REDEFINITION,
get_make REDEFINITION,
FINAL method 'get_n_o_veh_type' of superclass can't be redefined *
get_n_o_veh_type REDEFINITION,
All the methods of a FINAL class are implicitly FINAL. So there is *
no need to repeat the FINAL addition in these methods. *
display_car_availability.
PRIVATE SECTION.
DATA: gv_make TYPE string, " Vehicle make
gv_model TYPE string. " Type or model
ENDCLASS. "zl_lcl_car DEFINITION
&----
*& Class (Implementation) zl_lcl_car
&----
Implementation of Subclass
----
CLASS zl_lcl_car IMPLEMENTATION.
METHOD set_make.
*This method originally defined in the superclass is redefined in this *
*subclass. While redefining the method retains the same name and *
*interface, but has a new implementation. *
IF im_make IS NOT INITIAL
AND im_model IS NOT INITIAL.
CONCATENATE im_make '#' INTO gv_make.
CONCATENATE im_model '#' INTO gv_model.
ENDIF.
ENDMETHOD. "set_make
METHOD get_make.
*This method originally defined in the superclass is redefined in this *
*subclass. While redefining the method retains the same name and *
*interface, but has a new implementation. *
CONCATENATE gv_make '!' INTO ex_make.
CONCATENATE gv_model '!' INTO ex_model.
ENDMETHOD. "get_make
METHOD display_car_availability.
WRITE / 'Car of this make and model is available. '.
ENDMETHOD. "display_car_availability
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_veh_type_cnt TYPE i.
START-OF-SELECTION.
Create instance of the vehicle class
CREATE OBJECT z_car.
z_car->set_make( EXPORTING im_make = 'HYUNDAI'
im_model = 'SANTRO' ).
z_car->get_make( IMPORTING ex_make = gv_make
ex_model = gv_model ).
z_car->get_n_o_veh_type( IMPORTING ex_veh_typ = gv_veh_type_cnt ).
WRITE: / 'Vehicle Code :', gv_veh_type_cnt,
/ 'Make :', gv_make,
/ 'Model :', gv_model.
z_car->display_car_availability( ).
<b>Award points if useful.</b>
Regards
Indrajit
‎2007 Jul 06 11:41 AM
Hi Sandeep,
Final
Final classes cannot have subclasses.
They conclude an inheritance tree.
A final method cannot be redefined in a subclass.
All methods in final classes are automatically final.
Reward Points If useful.