Skip to Content

First of all i have also started learning the OO ABAPS a week ago and thought it would be beneficial for the beginners to understand the basic concepts of it. Also help.SAP.com provides very good articles and explanation on the topic, so please refer the following link:

http://help.sap.com/saphelp_47x200/helpdata/en/d3/2e974d35c511d1829f0000e829fbfe/frameset.htm

The Object Oriented Programming has three major Component around which i revolves. They are:

  • Polymorphism
  • Encapsulation
  • Inheritance

Polymorphism: In simple terms when you over write some functionality it’s called polymorphism. In polymorphism, you can inherit methods from the parent class and can modify it by implementing it again (which is nothing but implementation of the inherited method).

We will see now how we can achieve this;

1. Go to transaction SE24 and build a class and name whatever like this one: ZCL_POLYMORPHISM_SUPER

1.JPG

Please remeber not to

check the Final Checkbox. If you check this check box, any other class or sub-class cannot inherit from this class.

2. Go to Methods Tab and create a method under there:

2.JPG

3. Put cursor on

Select_Method method and press parameters button to enter parameters as shown:

3.JPG

4. Then click on the Methods with back arrow button and double click on select_method and enter the required code as shown:

4.JPG

5.

Put cursor on Display_Method method and press parameters button to enter parameters as shown:

5.JPG

6. Then go back to Methods by pressing the methods button, double click on the Display_Method and enter the required code as shown:

6.JPG

We are done creating the Super Class. Now let’s go and create the Sub-Class.

Go to t-code se24 and create the class: ZCL_POLYMORPHISM_SUB

Go to properties tab and press the SuperClass button and define the super class name we created:

7.JPG   

When we save the class then the methods along with parameters and logic are inherited in the sub class where the color of the inherited methods changes to blue color. We cannot change the methods, parameters and attributes of inherited class.

To redefine the existing functionality of the inherited methdod, we will put cursor on that method and press the Redifne button as shown:

8.JPG

Enhance the existing method by adding any write statement or anything like shown:

   CALL METHOD SUPER->SELECT_METHOD
  EXPORTING
    P_CARRID   =  P_CARRID
  IMPORTING
    WA_SFLIGHT = WA_SFLIGHT
    .
select single * from sflights into wa_sflights where carrid = p_carrid.

9.JPG

Create another method display_method1 with the exporting parameter wa_sflights, then double click on the method and implement the logic.

10.JPG

Create a program in se38 and provide the logic:

*Provide Object for Sub Class

data:obj1 type ref to ZCL_POLYMORPHISM_SUB.
*Provide Parameters

parameters: v_carrid type sflight-carrid.
*Provide Data Objects

data: wa_sflight type sflight.*     Wa1_sflights type sflights.

*Create Object instanceCREATE OBJECT OBJ1.
CALL METHOD obj1->SELECT_METHOD
 
EXPORTING
    P_CARRID   = v_carrid
 
IMPORTING
    WA_SFLIGHT = wa_sflight
    .

write:/ wa_sflight-CARRID,
        wa_sflight-CONNID.

WRITE: / obj1->wa_sflights-CARRID.

We are done with Polymorphism here.

Lets go and see what is Encapsulation.

Encapsulation : Wrapping up of data into single unit. Or, restriction on visibility of attributes and methods in the class. We have 3 levels of visibility:

     1. Private

     2. Protected

     3. Public

Methods or attributes defined as private are only visible and available to the class in which they are defined.

Methods or attributes defined as protected are visible to the class defined in and to the class which inherits from the class they are defined in.

Similarly the methods or attributes defined as public are available to all.

Lets start with an example again:

SE24 -> Create a Super class with name: ZCL_ENCAPSULATION

1.JPG

2.JPG

Create a Subclass ZCL_ENCAPSULATION_SUB by assigning super class

3.JPG

Now if you go and look in the attribute section, you will only be able to see the Public and Protected attribute. Private attaribute defined above is not visible as shown:

4.JPG

Similarly you will be able to see the methods of the super class but only the one which were declared as public and protected as shown:

5.JPG

THIS IS JUST A HIGH LEVEL VIEW ON HOW WE CAN SEE THE THINGS IN OO ABAP. DETAILED LEVEL WILL BE EXPLAINED LATER.

Now as we are done with Encapsulation, lets move on to Inheritance.

Inheritance: This can be defined from the word itself. That is to inherit properties from some parent class. Anything inherited will only be cisible if that is declared as public or protected in the super class.

Normally the OBJECT ORIENTED ABAp does not support the many to one inheritance, but this is made possible by using interfaces.

Interface is also a kind of class which contain the definitions only. Implementation of those defined methods will take part in the deriving classes only.

Lets goto SE24 and create and interface with name ZIF_INTERFACE.

Create a method SELECT_METHOD and define parameters for it as shown:

6.JPG

Now we will create a class named ZCL_INTERFACE and in the Interface tab of that class will put our interface name as shown:

7.JPG

The method will automatically be generated in the Methods tab as shown:

8.JPG

Double click on this method and write some logic like:

9.JPG

Now lets create an ABAP program and write the following code in that just to understand;

  DATA:sflight TYPE sflight,
     wa_sflight TYPE sflight,
     it_sflight TYPE z_sflight.
*providing data objects
DATA:obj TYPE REF TO zcl_interface.
*providing selection-screen
SELECT-OPTIONS:s_carrid FOR sflight-carrid.

START-OF-SELECTION.
  CREATE OBJECT obj.
  CALL METHOD obj->zif_interface~select_method
    EXPORTING
      p_carrid_low  = s_carrid-low
      p_carrid_high = s_carrid-high
    IMPORTING
      wa_sflight    = wa_sflight
      it_sflight    = it_sflight.

  DELETE ADJACENT DUPLICATES FROM it_sflight COMPARING carrid.
  LOOP AT it_sflight INTO wa_sflight.
    WRITE:/ wa_sflight-carrid,wa_sflight-connid,sflight-seatsmax.
  ENDLOOP.

The output comes as

10.JPG

So this is all, If I get some time, i will come up with one more document explaining something else.

Thanks,

Nitin Bhatia

iGATE Global Sol Ltd.

To report this post you need to login first.

10 Comments

You must be Logged on to comment or reply to a post.

  1. Matthew Billingham

    Nicely written, but given that it’s taken me several years of handon OO programming, to really begin to understand OO concepts, is it really wise to attempt to teach others when you are only a beginner yourself?

     

    You may be passing on misunderstandings to others.

    (0) 
    1. Nitin Bhatia Post author

      With all due respect Matthew, I would never want to do that. But please also let me know if I have gone wrong somewhere. We all could learn from your comments.

       

      Regards,

      Nitin.

      (0) 
      1. Matthew Billingham

        I’m sure you wouldn’t want to. I think your definition of polymorphism is not exactly wrong, but there’s more to it than that. Classes can be thought of as types. All classes implementing the same interface can be treated as having the type of that interface. All subclasses can be treated as having the type of the superclass.

         

        I don’t really want to discourage you, as your writing style is good, but I’d pitch it more as an exploration, than a definitive document.

        (0) 
        1. Nitin Bhatia Post author

          Thanks and wow Matthew, I could have never imagined that kind of definition. Surely experience teaches a lot. We would like to know more from you. I will visit the documents posted by you if any on oo abap to  boost up  my knowledge.

           

          Regards,

          Nitin.

          (0) 
          1. Francesco Amigoni

            Hi Nitin

            In regard of this document, I found it very nicely exposed AND perfectly fitting to begin the exploration of abap_oo enivronment (this was the simple objective of that post). I’ve followed your instructions and create my classes

             

            I surely know that abap oo is more than that, but this is definitely a good beginner’s guide.

             

            Regards.

            Franz

            (0) 

Leave a Reply