Gyan Factory

Gyan Factory
SAP Technical Project Support

Sunday, February 14, 2016

Final Method in Interface

Final method is a method which cannot be redefined by sub classes. So as in Interface final method can be declared and implemented similar to a class final method. Here the interface needs to be declared in a class as INTERFACE it FINAL METHODS mit2 where mit2 is the final method.

In the following program we have declared an Interface it where a text data and two methods mit1 and mit2 are there. Then we have defined a parent class where we have incorporated the interface with the final method mit2. In the implementation part we have given general statements to mit1 and call the mit1 method from mit2 which is final method. Now we have defined child class inheriting from parent class where we have redefined the method mit1. Since mit1 is not a final method we have implemented differently in child class. Finally under start of selection we have created parent class object obj_par and child class object obj_chi. Then we are calling the method mit2 by parent object and mit1 from child object. Hence mit2 shows the output declared in parent class mit1 method and secondly mit1 shows the output declared in child class mit1 method.

*&---------------------------------------------------------------------*
*& 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.
  PUBLIC SECTION.
    INTERFACES it FINAL METHODS mit2.
ENDCLASS.                    "parent DEFINITION

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

  METHOD it~mit2.
    CALL METHOD it~mit1.
  ENDMETHOD.                                                "it~mit2
ENDCLASS.                    "parent IMPLEMENTATION

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

*----------------------------------------------------------------------*
*       CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
  METHOD it~mit1.
    it~v_txt = 'Child Class Interface Method'.
    WRITE: / it~v_txt,
           / 'Today is', sy-datum DD/MM/YYYY.
  ENDMETHOD.                                                "it~mit1
ENDCLASS.                    "child IMPLEMENTATION

START-OF-SELECTION.
  DATA: obj_par TYPE REF TO parent,
        obj_chi TYPE REF TO child.

  CREATE OBJECT: obj_par, obj_chi.
  CALL METHOD: obj_par->it~mit2,
               obj_chi->it~mit1.


The following is the Output:

No comments:

Post a Comment