Skip to Content
Personal Insights
Author's profile photo Joachim Rees

How to create a Class in ABAP 00 – 6 Steps to start

This is not so much about which tool to use (AdT, SE24, VS-Code (is that a thing?)…) but more about what steps I take, when I want to program something (=> create a class).
And how I added more steps to the process while gaining more experience with OO.

(And: I think this is not even about ABAP…)

So it used to be just this:

1. Create the class.
2. Give it one public method.

But lately I added:
3. Create an interface.
4. Move the method to interface.

(Why have an interface? Because:
a. Its right thing to do (OO theory)
b. It helps me think about what is public and what not.
c. It helps with UnitTesting (Mocking) )

Also, I like to be able to start with unit tests right from the start (TDD maybe) so:
5. create test class. (using an adt template)
6. Add Interface and Class to testclass in setup, so the cut instance is created.

Now code away! 🙂

Do you have a similar list? (written down, or in your mind)
Care to share?
Am I missing something?


Edit: Here I show how I take those steps with an example:

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Michael Heidecker
      Michael Heidecker

      Why do you want to start with creating a class? OOP is for people who started of with OOP programming but never upgraded to serious application developers. In OOP you spend more time solving OOP problems then problems from the application domain . That's why it's appealing to OOP programmers to stick with OOP because they can devote lots of code to solve the same OOP related problems with inventing design patterns and antipatterns over and over again. The actual application domain is quite often secondary in the OOP code.

      OOP leads quite often to code which is inflexible to adapt to late changes in requirements in the development cycle (and they do occur!) when the change can not be implemented in the object model. When the object model reaches a certain size you start to fight the obejct orientation with object orientation by inveting helper classes, singeltons, setters, getters, unwanted dependencies on the inheritance tree etc adding layers and layers of code complexity which does not relate to your business problem. For others the original author the code gets unmanagable then.

      But most of all with OOP you can not do data centric development and in business application development this is much more efficient in both development effort and long term maintainability of the code. 

      When you use classical ABAP just avoid global variables and take care that all functions and procedures are idempotent you are far better off in development times, adaptability and maintainability of your code.





      Author's profile photo Joachim Rees
      Joachim Rees
      Blog Post Author

      Thanks for you comment!
      Especially the first part reminds me of something I was thinking about, but did not yet ask:

      Is it even the right thing to start creating a class as first thing, or should we do something else first? Like drawing an UML-diagram, maybe ...?

      Author's profile photo Bärbel Winkler
      Bärbel Winkler

      Joachim Rees

      Hi Joachim,

      I haven't yet created many classes from scratch - just two IIRC, actually. And I frankly, don't see the need to go overboard with creating global classes whenever I want to write a (straightforward) program which basically just gets some data from some tables, does some "massaging" of the data and then writes out the result as an ALV.

      What I have done instead was to create my own template program which works with local classes but does things like the selection screen handling the old way via the typical events and then triggers the local public classes from START-AT-SELECTION which always has the same look and feel:

        CREATE OBJECT lcl_main
            report_id = sy-repid
            no_input  = 1.
        IF sy-subrc EQ 0.
              "Check authority
              lcl_main->check_authority( ).
              "Select data from ....
              lcl_main->get_data( ).
              "Build output for ALV
              lcl_main->prepare_output( ).
              "Generate ALV-output
              lcl_main->display_data( ).
              "More logic as needed
            CATCH lcx_exception INTO zcx_exception.
              lv_result = zcx_exception->get_text( ).
              WRITE:/ lv_result.

      The public classes then call as many private classes as needed for the given requirement. Works pretty well and fast for me.



      Author's profile photo Joachim Rees
      Joachim Rees
      Blog Post Author

      Thanks for your input, Bärbel Winkler  !

      I have something quite similar with my AdT report template , and I still use that a lot.

      But nowadays I also quite often create global classes, and in this case I often use the steps above.