A final class is declared using the keyword FINAL in its definition. It’s used when you want to prevent further inheritance of the class, ensuring its behavior cannot be modified through subclassing.
Why Use a Final Class?
Reason | Explanation |
---|---|
Encapsulation | Prevents modification of core behavior. |
Security and Stability | Ensures the logic remains consistent, especially for critical functionality. |
Performance | ABAP runtime can optimize calls to final classes (minor performance benefit). |
Design Decision | Enforces design rule that a class should not be extended. |
*&---------------------------------------------------------------------*
*& Report ZREP_LCLCLASS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zrep_lclclass.
CLASS zcl_final_class DEFINITION FINAL.
PUBLIC SECTION.
METHODS: display.
ENDCLASS.
CLASS zcl_final_class IMPLEMENTATION.
METHOD display.
WRITE: 'This is a final class'.
ENDMETHOD.
ENDCLASS.
CLASS zcl_subclass DEFINITION INHERITING FROM zcl_final_class.
PUBLIC SECTION.
ENDCLASS.
Output :
while saving the changes