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:

5 comments:

Bhavya Kumar said...

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

Shruti Ramalingam said...

From My search…Creating Experts provides Best SAP Training with real time projects assistance.

Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage.

They also provide placement assistance in leading MNC companies across the globe according to the current requirements.

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

And these are the Best SAP training institute which provides Real Time Hands on Training…

codedion Technologies-9003085882

creating Experts-8122241286

http://thecreatingexperts.com/sap-mm-training-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

gilledashivani said...

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

SieveSoftware said...

Great Post. The information provided is of great use as I got to learn new things. Keep Blogging.
SAP ABAP TRAINING IN HYDERABAD