Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

final

Former Member
0 Likes
1,468

what is the significance of final keyword in abap,How can we avoid a class being inherited by another class

1 ACCEPTED SOLUTION
Read only

jaideeps
Product and Topic Expert
Product and Topic Expert
0 Likes
1,255

hi,

final keyword denotes that it is the final implementation for that method or variable or class. You can’t 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

5 REPLIES 5
Read only

jaideeps
Product and Topic Expert
Product and Topic Expert
0 Likes
1,256

hi,

final keyword denotes that it is the final implementation for that method or variable or class. You can’t 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

Read only

Former Member
0 Likes
1,255

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.

Read only

Former Member
0 Likes
1,255

Hi Sundeep,

If this answer was helpful , please reward with points.

Read only

Former Member
0 Likes
1,255

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

Read only

Former Member
0 Likes
1,255

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.