Monday, April 1, 2013

Abstract Method in Interface


Abstract method is a method which cannot be implemented in the same class. Only sub class can implement an abstract method by redefinition. Important point is that the abstract method can be declared in an abstract class only. Now Interface can have an abstract method which must be declared in an abstract class as INTERFACES it ABSTRACT METHODS mit2 where mit2 is the abstract method. So mit2 will have to be implemented in any sub class by redefinition.

In the following program we have declared an Interface it where a text data and two methods mit1 and mit2 are there. Method mit2 is an abstract method so it will be declared in an abstract class. Now we are defining parent class as an abstract class where we are declaring the abstract interface method with the statement of INTERFACES it ABSTRACT METHODS mit2. In the implementation part we have implemented mit1 in parent class because abstract method mit2 cannot be implemented in the same class. So we have defined child class inheriting from parent class. We have redefined the abstract method as METHODS it~mit2 REDEFINITION in this child class. And then we can implement this properly. Finally in the start of selection we call these two methods by the child class object since abstract parent class cannot be instantiated.

*&---------------------------------------------------------------------*
*& Report  ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       INTERFACE it
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE it.
  DATA v_txt TYPE char50.
  METHODS: mit1, mit2.
ENDINTERFACE.                    "it

*----------------------------------------------------------------------*
*       CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION ABSTRACT.
  PUBLIC SECTION.
    INTERFACES it ABSTRACT METHODS mit2.
ENDCLASS.                    "parent DEFINITION

*----------------------------------------------------------------------*
*       CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
  METHOD it~mit1.
    it~v_txt = 'Abstract Parent Class - Normal Method'.
    WRITE / it~v_txt.
  ENDMETHOD.                                                "it~mit1
ENDCLASS.                    "parent IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
  PUBLIC SECTION.
    METHODS it~mit2 REDEFINITION.
ENDCLASS.                    "child DEFINITION

*----------------------------------------------------------------------*
*       CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
  METHOD it~mit2.
    it~v_txt = 'Normal Child Class - Abstract Method'.
    WRITE / it~v_txt.
  ENDMETHOD.                                                "it~mit2
ENDCLASS.                    "child IMPLEMENTATION

START-OF-SELECTION.
  DATA obj TYPE REF TO child.
  CREATE OBJECT obj.
  CALL METHOD: obj->it~mit1,
               obj->it~mit2.


The output is following:


2 comments:

yektek training said...

thanks for sharing this information swathi

Unknown said...

Thanks for the great information in your blog SAP Success FactorsTraining in Chennai