Understanding Abstract Classes and Polymorphism

 

Abstract Class :-

  • In ABAP (Advanced Business Application Programming), an abstract class is a special type of class that cannot be instantiated on its own. Instead, it serves as a blueprint for other classes.
  • It is a special type of class which has at least one abstract method.
  • Abstract method is that method which has only definition, there will not be any implementation.
  • We can not create object of Abstract class.
  • We can create objects of sub classes of Abstract class.

Polymorphism :- Multiple Forms

  • Polymorphism is a characteristic of being able to assign a different behavior or value in a subclass, to something that was declared in Parent class.
  • Polymorphism, in the context of object-oriented programming, refers to the ability of different objects to respond in different ways to the same message or method invocation. This allows objects of different classes to be treated interchangeably through a common interface.
  • For example :- A method can be declared in a parent class, but each subclass can have a different implementation of that method.

Method Overloading :-

  • In ABAP, method overloading is not directly supported like in some other object-oriented programming languages such as Java or C++. However, you can achieve similar functionality using different method signatures.
  • Method overloading typically refers to defining multiple methods with the same name but with different parameters within the same class. The appropriate method to execute is determined based on the number and types of arguments passed to it during invocation.
  • Method Overloading is not allowed in SAP ABAP

Method Overriding :-

  • Method Overriding is performed in two classes with inheritance relationship ( superclass method and subclass method ).
  • In Method Overriding, methods must have the same name and same signature.

let’s take a real life scenario

Requirement :-

  • Create a abstract class with one abstract method and one non abstract method.
  • Then we need to create a subclass which will implement the abstract method.
  • Our requirement is that we require two checkboxes on Selection screen and when the User Select Sales Order then it should display Sales Order details from VBAK table and when user selects Billing, then should display billing details from VBRK table.

Solution :-

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



  • Step 2 :- Create a type structure for some fields from VBAK table and VBRK table and create a corresponding internal table for both structures.



  • Step 3 :- Create a selection screen which consists of one select option VBELN and two checkboxes.





  • Step 3 :- Create a abstract class with one abstract and one non abstract method.



  • Step 4 :- Create a child class as a Sub class of above created class which will implement the abstract method which was created above.



  • Step 5 :- In start of select create the object of child class.





  • Step 6 :- On the basis of checkbox, call the required method and display data using factory method of CL_SALV_TABLE class.






Code :-

*&---------------------------------------------------------------------*
*& Report ZAR_ABSTRACT_CLASS
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zar_abstract_class.

*&----------------------------------------
*&Create type strcture and their corresponding internal table
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.

TYPES : BEGIN OF ty_vbrk,
          vbeln TYPE vbeln_vf,
          fkart TYPE fkart,
          fktyp TYPE fktyp,
          vbtyp TYPE vbtypl,
          waerk TYPE waerk,
          vkorg TYPE vkorg,
        END OF ty_vbrk.

DATA : lt_vbak TYPE TABLE OF ty_vbak,
       lt_vbrk TYPE TABLE OF ty_vbrk.

SELECTION-SCREEN : BEGIN OF BLOCK b1.
  DATA : lv_vbeln TYPE vbeln_va.
  SELECT-OPTIONS : s_vbeln FOR lv_vbeln.

  PARAMETERS : p_chk1 AS CHECKBOX DEFAULT 'X' USER-COMMAND reset,
               p_chk2 AS CHECKBOX.

SELECTION-SCREEN : END OF BLOCK b1.

*&-------------------------------------------------------
*&Defining abstract class
CLASS lcl_abstract DEFINITION ABSTRACT.
  PUBLIC SECTION.
    METHODS : sales_order_display,
      billing_display ABSTRACT.
ENDCLASS.
CLASS lcl_abstract IMPLEMENTATION.
  METHOD sales_order_display.
    SELECT vbeln erdat erzet ernam vbtyp
      FROM vbak INTO TABLE lt_vbak
      WHERE vbeln IN s_vbeln.
  ENDMETHOD.
ENDCLASS.

*&---------------------------------------------------------
*&Creating a subclass.
CLASS child DEFINITION INHERITING FROM lcl_abstract.
  PUBLIC SECTION.
    METHODS billing_display REDEFINITION.
ENDCLASS.

CLASS child IMPLEMENTATION.
  METHOD billing_display.
    SELECT vbeln fkart fktyp vbtyp waerk vkorg
      FROM vbrk INTO TABLE
      lt_vbrk
      WHERE vbeln IN s_vbeln.
  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.
  DATA(lo_object) = NEW child( ).

  IF p_chk1 = 'X' AND s_vbeln IS NOT INITIAL.
    lo_object->sales_order_display( ).
    TRY.
        CALL METHOD cl_salv_table=>factory
          EXPORTING
            list_display = if_salv_c_bool_sap=>false
          IMPORTING
            r_salv_table = DATA(lo_disp1)
          CHANGING
            t_table      = lt_vbak.
      CATCH cx_salv_msg.
    ENDTRY.
    lo_disp1->display( ).

  ELSEIF p_chk2 = 'X' AND s_vbeln IS NOT INITIAL.
    lo_object->billing_display( ).
    TRY.
        CALL METHOD cl_salv_table=>factory
          EXPORTING
            list_display = if_salv_c_bool_sap=>false
          IMPORTING
            r_salv_table = DATA(lo_disp2)
          CHANGING
            t_table      = lt_vbrk.
      CATCH cx_salv_msg.
    ENDTRY.
    lo_disp2->display( ).

  ENDIF.

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

Execute the Program :-

1. For Sales Order

  • Select Sales Order Display checkbox.



    • Press F8.


    • Now go back.


2. For Billing

  • Select Billing Display checkbox



    • 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