Monday, April 1, 2013

Encapsulation by Interface

Encapsulation means one attribute and method can be modified in different classes. Hence data and method can have different form and logic and that can be hidden to separate class. 

Interface is used when we need to create one method with different functionality in different classes. Here the name of the method needs not to be changed. The same method will have to be implemented in different class implementations.

The following program contains an Interface it. There we have declared attribute and a method like mit. Now we have defined two classes like cls1 and cls2. So we have to implement the method mit in both of the class implementation. We have implemented the method mit differently in different class. Now in the start of selection we create two objects obj1 and obj2 for two classes. Next we call the method by different objects and we are getting the function declared in separate class.


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

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       INTERFACE it
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE it.
  DATA v_txt TYPE char50.
  METHODS mit.
ENDINTERFACE.                    "it

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

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

*----------------------------------------------------------------------*
*       CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
  METHOD it~mit.
    it~v_txt = 'Class One Interface Method'.
    WRITE / it~v_txt.
  ENDMETHOD.                    "it~mit
ENDCLASS.                    "cls1 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cls2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 IMPLEMENTATION.
  METHOD it~mit.
    it~v_txt = 'Class Two Interface Method'.
    WRITE / it~v_txt.
  ENDMETHOD.                    "it~mit
ENDCLASS.                    "cls2 IMPLEMENTATION

START-OF-SELECTION.
  DATA: obj1 TYPE REF TO cls1,
        obj2 TYPE REF TO cls2.
  CREATE OBJECT: obj1, obj2.
  CALL METHOD: obj1->it~mit,
               obj2->it~mit.


The output is following:

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