Monday, April 1, 2013

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 defined one interface under which two methods have been declared. Inside the class we mention the second method is final. Again we have declared a subclass and redefined the first method. Now at the time of calling, we call two methods from two different objects of two classes.


REPORT  zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       INTERFACE i_interface
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE i_interface.
  DATA v_test TYPE char40.
  METHODS: m_normal,
           m_final.
ENDINTERFACE.                    "i_interface

*----------------------------------------------------------------------*
*       CLASS cl_parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent DEFINITION.
  PUBLIC SECTION.
    INTERFACES i_interface FINAL METHODS m_final.
ENDCLASS.                    "cl_parent DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent IMPLEMENTATION.
  METHOD i_interface~m_normal.
    i_interface~v_test = 'I am normal Method from Parent class'.
    WRITE: /3 i_interface~v_test.
  ENDMETHOD.                    "i_interface~m_normal

  METHOD i_interface~m_final.
    i_interface~v_test = 'I am final Method from Parent class'.
    WRITE: /3 i_interface~v_test.
  ENDMETHOD.                    "i_interface~m_final
ENDCLASS.                    "cl_parent IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cl_child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child DEFINITION INHERITING FROM cl_parent.
  PUBLIC SECTION.
    METHODS i_interface~m_normal REDEFINITION.
ENDCLASS.                    "cl_child DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child IMPLEMENTATION.
  METHOD i_interface~m_normal.
    i_interface~v_test = 'I am redefined Method from Child class'.
    WRITE: /3 i_interface~v_test.
  ENDMETHOD.                    "i_interface~m_normal
ENDCLASS.                    "cl_child IMPLEMENTATION

START-OF-SELECTION.
  DATA: obj_parent TYPE REF TO cl_parent,
        obj_child  TYPE REF TO cl_child.

  CREATE OBJECT: obj_parent,
                 obj_child.

  CALL METHOD: obj_parent->i_interface~m_normal,
               obj_parent->i_interface~m_final,

               obj_child->i_interface~m_normal,
               obj_child->i_interface~m_final.

The following is the Output:

3 comments:

Unknown said...

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

Camelliacanan said...

Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..

SAP HR Training in Chennai

SAP SD Training in Chennai

SAP Basis Training in Chennai

Business World said...

Thanks for sharing valuable information through your post. The information provided is of great use.
SAP ABAP TRAINING IN HYDERABAD