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:


4 comments:

yektek training said...

thanks for sharing this information swathi

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/

suganya said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

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

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!