Monday, April 1, 2013

Interface - Type of ABAP Objects

Let us suppose we have two abstract classes, class A & class B. Now we know that the abstract class must contains at least one abstract method. Hence more than one abstract classes (here A & B) have abstract methods in each of those classes.

Now we want to implement those abstract methods. To do that we need to create a subclass of those abstract classes A & B. So a new class C will be created which is a subclass of A & B. This situation is called multiple inheritances. Unfortunately ABAP doesn’t support multiple inheritances.

To use this multiple inheritances functionality we need to use Interface.

Let us suppose we have two interfaces (A & B) rather than the classes. Now we can create a class C which implements these interfaces. When the relation is built by class to class, it needs inheritance. But when relation is built by interface to class it needs implements.

Now in this C class we implement all the abstract methods which have been declared in interfaces (A & B). After that we create an object of class C and then we can use it.

Most important point is that we can’t instantiate interface. It means we can’t define anything (like methods, constructor etc.) in interface. So to access the interface we need to create a normal class which implements those interfaces and then we can instantiate it.

Moreover we can refer the interface with the normal class. This is because to refer something there is no need to create object. Let’s take the example of JAVA.

We have interfaces A & B and we have normal class C.
To instantiate the class C: C obj = new C( );
Here we are defining the default constructor C( ). But this will not happen for creating object of interface. Here we can refer the interface with the normal class C like:
A objA = new C( );
B objB = new C( );
This is possible because we are using the memory of constructor C to create the reference of interface. It will work as a normal object. But this reference will have no access of class C’s methods. It can only use its own declared methods.

In the following program we have declared two interfaces i_a & i_b. Each of those contain own data and methods. Now we are declaring a normal class where we define normal data & methods. Then we declare the interfaces.

INTERFACES: i_a,
                  i_b.

At the implementation point we define all the methods (own method and interface methods also). The interface method is defined like this.

  METHOD i_a~m_inta.
    i_a~v_inta = 'I am from A Interface method'.
    WRITE: /3 i_a~v_inta.
  ENDMETHOD.                    "i_a~m_inta

After start of selection at the time of calling methods we need to call by this way.

  CALL METHOD: obj->m_test,     "Calling normal method
               obj->i_a~m_inta, "Calling interface method
               obj->i_b~m_intb. "Calling interface method

Whole program is as follows.

REPORT  zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       INTERFACE i_a
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE i_a.
  DATA v_inta TYPE char40.
  METHODS m_inta.
ENDINTERFACE.                    "i_a

*----------------------------------------------------------------------*
*       INTERFACE i_b
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE i_b.
  DATA v_intb TYPE char40.
  METHODS m_intb.
ENDINTERFACE.                    "i_b

*----------------------------------------------------------------------*
*       CLASS cl_c DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_c DEFINITION.
  PUBLIC SECTION.
    DATA v_test TYPE char40.
    METHODS m_test.
    INTERFACES: i_a,
                i_b.
ENDCLASS.                    "cl_c DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_c IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_c IMPLEMENTATION.
  METHOD m_test.
    v_test = 'This is normal method'.
    WRITE: /3 v_test.
  ENDMETHOD.                    "m_test

  METHOD i_a~m_inta.
    i_a~v_inta = 'I am from A Interface method'.
    WRITE: /3 i_a~v_inta.
  ENDMETHOD.                    "i_a~m_inta

  METHOD i_b~m_intb.
    i_b~v_intb = 'I am from B Interface method'.
    WRITE: /3 i_b~v_intb.
  ENDMETHOD.                    "i_b~m_intb
ENDCLASS.                    "cl_c IMPLEMENTATION

START-OF-SELECTION.
  DATA obj TYPE REF TO cl_c.
  CREATE OBJECT obj.

  CALL METHOD: obj->m_test,     "Calling normal method
               obj->i_a~m_inta, "Calling interface method
               obj->i_b~m_intb. "Calling interface method
  SKIP.
  "Executing interface data
  WRITE: /3 'Interface Data: ', obj->i_b~v_intb.

The output is as follows:

11 comments:

Jayaprakash said...

Thanks u so much.!! Was very helpful.

yektek training said...

nice post thank you

bytesonlinetraining said...



this is very good nice article. this is very useful for SAP ABAP students.


==========================================================
This is very nice article. This is very use ful for SAP ABAP Learners.
http://www.bytesonlinetraining.com/sap-abap-online-training/
===========================================================

hi sir. i want to do SAP ABAP training.
SAP ABAP ONlINE TRAINING Thanks for providing

valuable information.

Anonymous said...

thank you vry much pls post how to upload an excel file to webdynpro

Anonymous said...

This blog is really helpful.... Thanks a lot Sandip and plz keep posting

Raju Kumar said...

I used to be able to find good info from your content.
SAP APO Online Training

Bhavya Kumar said...

Best blog.Got to learn new things.Thanks for this Blog SAP Success Factors Training 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!!!!!!

gilledashivani said...

Great information. the information provided is of great use as i got to learn new things. If you are looking for real time training in SAP ABAP, kindly contact sieve software
Sieve software offers real time training by industry experts with live projects and placement support.
SAP ABAP TRAINING IN HYDERABAD

akhila priya said...

The information which you have provided is very good. It is very useful who is looking for Java online training Bangalore