Вы находитесь на странице: 1из 19

Just 4 Versions of the same program to understand

OOPs ABAP
TOPICS: ABAP Objects

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.

1
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

2
Highlights of the above program which is no more used.

3
Let us test this program.

Classical Output Report


4
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

5
6
Check the output has changed from classical to ALV.
7
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.

Program Version 3.0

8
9
10
11
Now let us look at the obvious flaws.

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
Clients SAP system. It is not oversimplified for the demo.

12
Check this OOPs program. Doesnt it look clean?

Program Version 4.0

Details of the classes used above in the program.

13
1. Class: YCL_REPORT_SELECTION

14
2. Class YCL_REPORT_FETCH

15
3. Class YCL_REPORT_DISPLAY

16
17
The output is the same.

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.

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

19

Вам также может понравиться