Just 4 Versions of the same program to understand OOPs ABAP

Object Oriented Programming
Share on Facebook97Share on LinkedIn112Tweet about this on TwitterShare on Google+0
Please Share!!

We will modify an ABAP program 4 times today and make it an OOPs ABAP program at the 4th Version. Please stay until the end and I promise, you will learn how to program using Object Oriented Approach in ABAP.

I have been programming in ABAP for more than 10 years now but hardly have I designed an end to end development in OOPs ABAP. I have used classes (local/global), methods (static/instance), attributes etc but I cannot claim I am an OOPs ABAP developer. I am still the same old developer who is used to the procedural language. I have worked extensively in Web Dynpro ABAP, which has MVC as the core Architecture and I have followed the principle to the core. But 90 percent of the time I have used the wizard to generate my code in Web Dynpro. Experts suggest we should write those code ourselves. But Lazy ABAPers always use the wizard, after all, why is that magical wand(wizard) provided in the first place.

I understand the concept of OOPs and I do not want to re-iterate the Vehicle examples here. But even though I have not used OOPs extensively, down at the bottom of my heart I know that OOPs if written correctly is better and maintenance is simpler as opposed to the theory of procedural enthusiasts like me who believe OOPs is difficult to maintain. 🙂

We always give the excuse that the existing program is not in OOPs so we do not want to introduce OOPs while supporting existing object. The other famous excuse is, the deadline is so aggressive. So let me not venture into OOPs and do the way I know better. 🙂 . With this mind set, I was never able to learn OOPs. But, now I am trying to amend myself. I am trying to write the new developments in OOPs even though it might have flaws. It is never too late to learn. If I look back to the tons of codes which I have written in my initial days using procedural approach, I can point out numerous issues in those so called simple approach. So today, if there is a problem with my OOPs approach, it would just be a learning curve. Tomorrow if I look back to these OOPs programming and I can point out the mistakes, then I should consider I have learned some OOPs then. For some reason, if I am not able to find fault in my OOPs then probably, I never ever programmed in OOPs.

Let me pull a real object from one of my project. We had a report to display all the Roles assigned to any Transaction code which Security Team and Business Team could execute to plan the authorization for different users. Check the program which displays classical report output and internal tables with header line were still the practice then when it was written.

Program Version 1.0

Highlights of the above program which is no more used.

ABAP Technical

Let us test this program.

ABAP Objects

Classical Output Report

OOPs ABAP

If you have some time, please go through this beautiful (a little long) and eye opener article on SCN. Crossing the Great Divide – Procedural vs OO ABAP Programming

Now let us check the same program which was written by a programmer who is acquainted with REUSE_ALV_GRID_DISPLAY and who does not declare the internal table with HEADER LINE.

Program Version 2.0

Also check: Video Refreshers on ABAP OO Programming (15 minutes videos per session)

Check the output has changed from classical to ALV.

Object Oriented Programming in SAP ABAP

Now, let us check how most of the old-schooled ABAPers try their OOPs programming when they are forced to adopt OOPs. I know some projects where OOPs is the only standard of programming. In such projects, the not so OOPs enthusiast replace PERFORMs by METHODs without really taking the advantage of Object Oriented Architecture. Check how we can really kill the beauty of OOPs below. And I am one of them. 🙂

Also Read: Web Dynpro ABAP Tutorials

Program Version 3.0

Now let us look at the obvious flaws.

Object Oriented Programming in ABAP

Even if you start OOPs programming the above way and you know about the mistakes while writing your second OOPs program, you are on the right path of OOPs. Knowing our own mistake is the first step in learning. The senior mentors and juniors who are expert in OOPs approach should be our guide and pathfinder. Asking for help in understanding the concept should never make us feel ashamed. And no question is too trivial even though you have 10 years of experience. Everyone is learning and it is never too late.

Interested in SAP ABAP for HANA? Browse our ABAP on SAP HANA Tutorials.

Let us check the better way of writing the same program in OOPs. There might be issues in this code as well, so I humbly request the experts to correct me and show the right way.

I have created three classes in SE24 to simulate the MVC. The selection screen parameters and validation are done in one class. The data retrieval is done in the second class. The report is displayed using the third class. Also, the objects from one class are leveraged in the next class and so on. This program might not show the real arsenal of OOPs but it should be sufficient to showcase what we intend. This is a real program in a real project in a real Client’s SAP system. 🙂 . It is not oversimplified for the demo. 😛

Check this OOPs program. Doesn’t it look clean?

Program Version 4.0

Details of the classes used above in the program.

1. Class: YCL_REPORT_SELECTION
ABAP Object Concept

2. Class YCL_REPORT_FETCH

OOPs programming in ABAP

3. Class YCL_REPORT_DISPLAY

Introduction to OOPs ABAP

The output is the same. 🙂

Constructor in OOPs

Did you check our popular post? Lazy and Smart ABAPers.

Exercise: 

Put breakpoints on the CONSTRUCTOR methods of classes YCL_REPORT_FETCH and YCL_REPORT_DISPLAY and also at the INITIALIZATION of the report. Run your report.

What do you see?

As expected, the CONSTRUCTOR method of the classes would be called at the INITIALIZATION event of the report. Check, how the object data is passed from one class to another in this report and used in other methods (FETCH_DATA and DISPLAY_ALV).

If you want to know the difference between STATIC and INSTANCE method or have fun with CONSTRUCTOR. Read : ABAP Objects Tips

If I start OOPs today, I might be able to grasp the real concept and appreciated its actual power and usage in a couple of years. Till then let me learn from my mistakes.

Hope this post would act as a launching pad to all those ABAPers who were looking for a starting point to begin their OOPs journey. If you are still in a dilemma as of how to start programming in OOPs ABAP, my suggestion is, just pick any existing report from your system and try to convert it to OOPs. Ask your team member or ask SAP mentors or leave your doubts in SCN. Experts would definitely guide you or at least take you to the right link where you can learn further.

If you have any examples of vanilla ABAP program and the new OOPs program for the same, please do share it with us. We would be happy to append it to this post so that it might act as a reference to all those ABAPers who might visit this page by mistake or knowingly. 🙂

Let us try to change our flavor and force ourselves to love the new taste of Object Oriented Programming in ABAP.

If you want to get such useful articles directly to your inbox, please SUBSCRIBE. We respect your privacy and take protecting it seriously.

If you liked this post, please hit the share buttons and like us on facebook.

Do you have anything to add to this article? Have you faced any issue using OOPs? Do you want to share any real project requirement or solutions? Please do not hold back. Please leave your thoughts in the comment section.

Thank you very much for your time!!

Did you check our below popular Tutorials?

1. ABAP for SAP HANA Tutorials
2. ABAP Web Dynpro Tutorials
3. GOS Tutorial
4. OOPs ABAP Tutorial
5. HANA Tutorial
6. SAP Netweaver and OData Tutorial
7. SAP Adobe Form Tutorial
8. SAP Fiori Tutorial
9. SAPUI5 Tutorial

 

Share on Facebook97Share on LinkedIn112Tweet about this on TwitterShare on Google+0
Please Share!!

About the Author

SAP Yard
SAP Yard

SAPYard is one stop page for all Technical Folks in SAP. You would find un-conventional explanations, tutorials, and tricks.

Please like our Facebook Page and also join our LinkedIn Group.

17 Comments on "Just 4 Versions of the same program to understand OOPs ABAP"

  1. Nice post Raju. Thanks for sharing.

  2. Hi,

    In method display_alv you refer to class cl_demo_output but it doesn’t exist in my system. What should I use for it?

  3. Good Day Team SAPYard.
    Though I’m considered myself a procedural type of guy, I’m still yearning to learn the power of OOP. Emphasizing on comparison between OOP and Procedural module would certainly helps a lot to those who want to dig deeply in OOP.
    Thanks for this great tool.

    • You are welcome Feleciano!!

      We both are in the same boat, i.e. Procedural programmer with some concept of OOPs and trying to apply OOPs in real projects.

      Comparison of known Procedural style with OOPs is a good way to start, therefore we published this simple article. Hoping, it would be useful to Freshers and those who are new to OOPs ABAP.

      Regards,
      Team SAPYard.

  4. Why you have used SELECT COUNT(*) UP TO 1 ROWS? This may not be a very good approach if you want to just check if the record exist or not. In some DB, even if COUNT (*) UP TO 1 ROW is used, it may fetch all records and return you one record. Also, COUNT will always bypass buffer. If all keys are available, use SELECT SINGLE or SELECT UP TO 1 ROW.

    • Dear Maloy – Thank you very much for stopping by.

      We were told, it would fetch just 1 and return. We have been suing COUNT(*) UP TO 1 ROWS as there is no need to declare an extra variable/work area to keep that 1 row/record which we need while doing SELECT SINGLE or SELECT UP TO 1 ROWS.

      If COUNT(*) UP TO 1 ROWS internally fetches more than 1 entry, then we should definitely avoid it.

      Thank you again.

      Regards,
      Team SAPYard.

  5. Warren Bjorkman | September 26, 2016 at 1:18 pm | Reply

    I am a bit of an old style programmer, and not very bright, but the first program which outputs the same data as the last, is only 60 lines long, is simple and fast to code, easier to read and to see what is going on all in one place, and also a whole lote easier to debug to find potential errors, in my opinion.
    Or maybe i am lazy and not smart, but this is why i tend to stick to linear programming although i do sometimes maintain the code of others who have written in OO format.

    • Dear Warren,

      Thank you very much for visiting our page and leaving your question.

      True, the first program is short and easy to understand and debug. For straight forward requirements like this where we do not use all the powers (inheritance etc) of OOPs, we will not be able to justify the usage of OOPs.

      The lines of code might look more in the class, but most of the code in the class are auto generated. You just need to write logic for your SELECT and DISPLAY. Data declaration and Types etc are auto generated when you create the Class in SE24 and define your Attributes.

      Moreover, we can reuse the global class in other Reports.

      The real advantage of OOPs can be seen in projects where they have global roll outs. Same thing done for different language or currency or time etc. Or same thing done with little exception for another country in the same project.

      In those scenarios, we will not be creating multiple reports for multiple country roll outs. In those cases the OOPs program would in fact be small.

      You might like to read this interesting (but long) scn post at your free time. 🙂

      http://scn.sap.com/community/abap/blog/2012/10/27/crossing-the-great-divide–procedural-vs-oo-abap-programming

      Experts say, if programs are written in OOPs, it is easier to maintain. 🙂 Not sure if you would agree.

      By the way, I am also a linear/procedural ABAP language developer, who is try to learn how to swim in OOPs water. 😛

      Regards,
      Team SAPYard.

  6. Praveen Kumar Bindla | September 26, 2016 at 3:41 am | Reply

    Thanks Sap yard ,
    My query is cleared.
    But u used fetch class attribute in display , that’s fine.
    But you did not used selection class in display , but why you passed that selection reference .

    Thanks

    • Dear Praveen – We are happy that we were able to clarify your question.

      We are not passing SELECTION class explicitly to DISPLAY.

      Please check below. SELECTION is passed to FETCH and FETCH is passed to DISPLAY.

      CREATE OBJECT o_fetch EXPORTING io_rep_sel = o_selection.
      CREATE OBJECT o_display EXPORTING io_rep_fetch = o_fetch.

      SELECTION attributes are used in FETCH and FETCH attributes (in this case the final internal table) is used in DISPLAY.

      Not sure, if we were able to make you understand. Please feel free to re-frame your question. We would be happy to reply.

      Regards,
      Team SAPYard.

  7. Praveen Kumar Bindla | September 26, 2016 at 3:07 am | Reply

    Why you used selection class in constructors of fecth and display classes?

    • Dear Praveen – Thank you for your visit and question.

      Selection Class is called in constructor of FETCH and Fetch Class is called in DISPLAY. This is to demonstrate the OOPs concept where one CLASS is declared as an Attribute in another CLASS.

      For example, SELECTION Class is an Attribute in FETCH and FETCH Class is an Attribute in DISPLAY Class.

      This provides us access to the Public Attributes (variables) thus gaining access to Data contents in another Class. We do not need to pass individual attributes/variable to calling class. Just declare the called Class as Attribute and you get everything which is public.

      Hope it is clear. Do let us know if you need further clarification

      Regards,
      Team SAPYard.

  8. Shashikant Wadhavane | September 22, 2016 at 9:24 am | Reply

    As mentioned,doesn’t focuses on main concepts and power of OOPs but Good exercise for new OOPs Developer for sure to start with.. 🙂

    • Thank you Shashi. Yes, that was our idea. Not to preach about concepts and theory but to show a real time example which would be self explanatory..

      Glad you liked it.

      Please keep visiting and keep providing your valuable feedback.

      Regards,
      Team SAPYard.

Leave a Reply to Praveen Kumar Bindla Cancel reply

Your email address will not be published.


*