Monday, April 1, 2013

Interface can be Instantiated


Interface can be instantiated if the object is referenced to that interface. Here the class instance variable will have to be moved to the interface instance variable.

Following is a program where we have declared an interface containing constant, data and a method. Now the class definition contains only the interface in the public section. After that the method is implemented accordingly. In the start of selection we have created objects of the class. Here we have moved the class object variable to the interface variable and then we have called the method directly from the interface object variable.

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

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       INTERFACE it
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE it.
  CONSTANTS c_var TYPE char40 VALUE 'SAP ABAP Object Oriented'.
  DATA var TYPE char50.
  METHODS mit.
ENDINTERFACE.                    "it

*----------------------------------------------------------------------*
*       CLASS cls DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION.
  PUBLIC SECTION.
    INTERFACES it.
ENDCLASS.                    "cls DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.
  METHOD it~mit.
    it~var = 'Interface Attribute'.
    WRITE: / 'Interface Method',
           / it~var,
           / it~c_var.
  ENDMETHOD.                    "it~mit
ENDCLASS.                    "cls IMPLEMENTATION

START-OF-SELECTION.
  DATA: obj_it TYPE REF TO it,
        obj    TYPE REF TO cls.
  CREATE OBJECT obj.
  obj_it = obj.
  CALL METHOD obj_it->mit.


The following is the output:


4 comments:

yektek training said...

Very nice information given by you thank you

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!!!!!!