Personal Insights
How to create a class in ABAP 00 – example code, with little typing. AdT is great!
In my post https://blogs.sap.com/2022/02/24/how-to-create-a-class-in-abap-00-6-steps-to-start/ I explained what steps I take when I create a new ABAP-OO class.
Here I show those steps with an example.
I will also demonstrate how AdT helps me a lot, in the sense of creating code for me, so I don’t have to type it.
Here we go: Log in to your AdT and:
1. create a class
ctrl+shift+n -> new class
CLASS zcl_temp_delete_demo DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_temp_delete_demo IMPLEMENTATION.
ENDCLASS.
2. add a method:
PUBLIC SECTION.
METHODS: do_something_good.
ctrl+1 to add the implementation.
3. add an interface:
PUBLIC SECTION.
INTERFACES zif_temp_delete_demo.
ctrl+1 to create the interface.
interface ZIF_TEMP_DELETE_DEMO
public .
endinterface.
4. Move the Method to interface.
ctrl+1 on the method definition -> pull-up to the interface.
5. create test class.
Switch to Test Classes tab.
Create the test-Class with a template (still on my todo list: write a little blog about my text class template)
CLASS ltc_test_something DEFINITION FOR TESTING
RISK LEVEL HARMLESS
DURATION SHORT.
PRIVATE SECTION.
data: cut type ref to ZCL_TEMP_DELETE_DEMO.
METHODS: test1_test_something FOR TESTING,
setup.
ENDCLASS.
CLASS ltc_test_something IMPLEMENTATION.
METHOD setup.
cut = new ZCL_TEMP_DELETE_DEMO( ).
ENDMETHOD.
METHOD test1_test_something.
"given
"when
"then
ENDMETHOD.
ENDCLASS.
6. Add Interface and Class to test class, so the cut instance is created.
This actually already comes from my adt-template, so I only have to change the defintion of cut to refer to the interface instead of the class:
data: cut type ref to ZIF_TEMP_DELETE_DEMO.
Those where the 6 steps.
So what I got now is
1. this code:
CLASS zcl_temp_delete_demo DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES zif_temp_delete_demo.
ALIASES: do_something_good FOR zif_temp_delete_demo~do_something_good.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_temp_delete_demo IMPLEMENTATION.
METHOD zif_temp_delete_demo~do_something_good.
ENDMETHOD.
ENDCLASS.
And
2. this test code:
CLASS ltc_test_something DEFINITION FOR TESTING
RISK LEVEL HARMLESS
DURATION SHORT.
PRIVATE SECTION.
data: cut type ref to ZCL_TEMP_DELETE_DEMO.
METHODS: test1_test_something FOR TESTING,
setup.
ENDCLASS.
CLASS ltc_test_something IMPLEMENTATION.
METHOD setup.
cut = new ZCL_TEMP_DELETE_DEMO( ).
ENDMETHOD.
METHOD test1_test_something.
"given
"when
"then
ENDMETHOD.
ENDCLASS.
It’s not so much – it does not do anything, yet. But it’s a solid, clean foundation. And please note how few typing I had to do: more or less only the names of class, method and interface:
zcl_temp_delete_demo
METHODS: do_something_good.
INTERFACES zif_temp_delete_demo.
jre ( thats how my adt-Template starts. )
if (replacing the CL in ZCL_TEMP_DELETE_DEMO).
-> Thats all. All the other lines where created using AdT-Quick-Fix and a AdT Template.
This leads me to say:
1. AdT is awesome.
2. You can start with a great code foundation, without loosing much time.
Do you have a similar workflow?
How do you go about starting to code something?
best
Joachim
For part 5 you can use the in-built AdT template testclass, no need for a custom template.
Thanks for the hint, Jonathan Bourne ! I'll check it out!
1, Create interface
2. Create factory class with GET_INSTANCE returning an instance of the interface (no code in that method yet though!
3. Create class to implement interface CREATE PRIVATE, make factory class a friend.
4. Ctrl-1 add stub methods for interface
5. In factory class r_result = new my_class( ).
(I got this from Clean ABAP).