Understanding Inheritance and Final Classes in ABAP: A Practical Guide with Examples

 

Inheritance :-

  • Inheritance is creating a Sub class from Parent class.
  • The concept of Inheritance allows us to derive new classes from existing classes.
  • Inheritance allows a class to inherit attributes and methods from another class.
  • In ABAP, you use the INHERITING FROM keyword to specify the superclass from which a subclass will inherit.

Final Class :-

  • A final class is that class which can not be inherited.
  • A final class can not be inherited.
  • In ABAP, you can mark a class as final using the FINAL keyword in the class definition.
  • A final class cannot be subclassed, meaning you cannot create subclasses that inherit from a final class.

Note :-

  • If we pass a final class as a Super class then we will get the error, i.e. Your class is final, It cannot have subclasses.




let’s take a requirement to understand the Inheritance.

Requirement :-

  • We need to create one parent class and one child class.
  • We will define one method to display the Sales Order display using VBAK table.
  • We need to declare one select option for Sales Document Number and we will display the Sales Order Details from VBAK table.
  • All logic will be written in parent class and we will create the object of child class and fulfill our requirement.

Solution :-

  • Step 1 :- Create a executable program in ABAP Editor ( SE38 ).



  • Step 2 :- Define a type structure for few fields from VBAK table and create a internal table and work area for the same.



  • Step 3 :- Define a select option to take user input.



  • Step 4 :- Parent Class definition with one method display Sales Order Details.



  • Step 5 :- Parent Class Implementation.



  • Step 6 :- Create a Child Class which is inheriting the above created Parent Class.



  • Step 7 :- In Start Of Selection create the object of child class and call the method of Parent Class from Child Class.



  • Step 8 :- Use factory method of CL_SALV_TABLE class to display the output in form of ALV.




Code :-

*&---------------------------------------------------------------------*
*& Report ZAR_INHERITANCE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zar_inheritance.

TYPES: BEGIN OF ty_vbak,
         vbeln TYPE vbeln_va,
         erdat TYPE erdat,
         erzet TYPE erzet,
         ernam TYPE ernam,
         vbtyp TYPE vbtypl,
       END OF ty_vbak.

DATA : lt_vbak TYPE TABLE OF ty_vbak.

*&----------------------------------------------
*&Select Option for User Input
DATA : lv_vbeln TYPE vbeln_va.
SELECT-OPTIONS : s_vbeln FOR lv_vbeln.

*&----------------------------------------------------
*&Parent Class Definition.
CLASS parent DEFINITION.
  PUBLIC SECTION.
    METHODS : display_sales_order_details.

ENDCLASS.

*&-------------------------------------------
*&Parent Class
CLASS PARENT IMPLEMENTATION.
  METHOD display_sales_order_details.
    select vbeln erdat erzet ernam vbtyp
      from vbak
      into table lt_vbak
      where vbeln in s_vbeln.
  ENDMETHOD.
ENDCLASS.

*&-------------------------------------------
*&Creating the Child Class.
Class Child DEFINITION INHERITING FROM PARENT.

ENDCLASS.

START-OF-SELECTION.
DATA(lo_object) = new Child( ).
lo_object->display_sales_order_details( ).

TRY.
CALL METHOD cl_salv_table=>factory
  EXPORTING
    list_display   = IF_SALV_C_BOOL_SAP=>FALSE
  IMPORTING
    r_salv_table   = data(lo_disp)
  CHANGING
    t_table        = lt_vbak
    .
  CATCH cx_salv_msg.
ENDTRY.
lo_disp->display( ).

*&-----------------------------------------------------------------
*&End of  Program
*&----------------------------------------------------------------

Execute the Program :-



  • Press F8.



Comments

Popular posts from this blog

Understanding Different Types of SAP Function Modules: Normal, RFC, and Update

Unlocking SAP ABAP Secrets: Interactive Classical Reports with GET CURSOR, At User Command, and a Comparison of HIDE Statements

Mastering ABAP: A Step-by-Step Guide to Function Modules and Groups for Modularized Programming