Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Shivani11
Explorer
15,791

Introduction


I'm writing this blog to provide an overview of how to define the ABSTRACT CLASS in Local Class & Global


Class. Many beginner developers working in SAP ABAP OOPs, need to be familiar with OOPs concept. One of the concepts is ABSTRACT CLASS & METHOD is explained here.


Readers will be able to understand how to create Abstract Class in Global Class & Local Class as well.


 

Part 1 - Define Abstract Class in Global Class.


Part 2 - Define Abstract Class in Local Class.


In this blog post, I hope readers can learn:




  1. What is Abstract Class?

  2. Where to use Abstract Class?

  3. How to define abstract in SAP ABAP?


What is an ABSTRACT CLASS?


Abstract Class is a special type of class which has at least one abstract method. Abstract method is that method which has only definition, this means it does not contain implementation of the method Only a Declaration. We can create object of only subclass of Abstract Class.

Note: We cannot create object of Abstract Class.

Where Abstract Class Should be used?



  1. As Abstract Class is flexible in nature, so we can use to create multiple flavors of the object with some default operations.

  2. Abstract Class should be used for those classes which are closely related to each other.

  3. Abstract Class allows you to partially implement your class.


How to create Global Abstract Class?


Steps1:  Use Tcode SE24 to create Abstract Class. Enter the name of Abstract Class and click on Create button.


Step2: By default, Class will be selected. Press Enter/Green Button.


Step3: Enter the description of class. Make sure that Final checkbox should be unchecked.

Note: We cannot create object of Abstract Class, so we have to create subclass for this parent class. Final Class will not allow us to create the child class.


Step4: Save the Class in local object or if we have any particular package, then mention the package in Package field.


Step5: Create an instance method in Method tab. Keep your method as Public.


Step6: Define the parameters for your method.

As I want to display details like first name, second name, city, house number of customer w.r.t the customer number from table KNA1.


Step7: For creating any method as abstract: Click on Goto properties. A new pop up will open.

There you have to select Abstract checkbox and click on Change button.


Step8: Once you select the Abstract checkbox, one more popup will appear. Click on green button.


Step9: Once you click on green button, method will be set to Abstract method.

Step10: Now we have to create a child class for this Abstract class, so that we can write the logic for the method. Repeat Step 1, Step 2, Step 3 & Step 4 for creating a class.

Step11: Go to properties tab. Select Superclass option. Enter the name of abstract class in superclass field. Press enter key.

Note: As you press enter key, it will show the same method in method tab (which is present in Abstract class).


Step12: Still the Display Method is Abstract method. It means we cannot write the logic. For this we need to redefine the method. For redefining the method, select redefine tool.


Step13: Now we can write the logic.


Step14: Create a class in SE38 and call the method.
REPORT ZABSTRACTCLASS_7.
Parameters: p_kunnr type kunnr.
data: object1 type ref to zabstract_child_7.
data: lv_first_name type NAME1_GP,
LV_SECOND_NAME type NAME2_GP,
LV_CITY TYPE ORT01_GP,
LV_HOUSE_NO TYPE STRAS_GP.
CREATE OBJECT OBJECT1.
CALL METHOD OBJECT1->DISPLAY
EXPORTING
IV_CUST_NO = P_KUNNR
IMPORTING
EV_FIRST_NAME = LV_FIRST_NAME
EV_SECOND_NAME = LV_SECOND_NAME
EV_CITY = LV_CITY
EV_HOUSE_NO = LV_HOUSE_NO
.
WRITE:/ 'DETAILS ARE:;',
/ 'FIRST NAME:', LV_FIRST_NAME,
/ 'SECOND NAME:', LV_SECOND_NAME,
/ 'CITY NAME:', LV_CITY,
/ 'HOUSE NUMBER:', LV_HOUSE_NO.

SUMMARY


In summary, developers can write more modular, flexible, and maintainable code by leveraging OOP concepts like abstract in SAP ABAP. Abstract Class is used to write different logic in different derived classes instead of making change in original class. If we make the changes in the original class, then it will affect all the programs where we had used that class. We can create different classes for each scenario but to achieve reusability feature of OOPs, then abstract class comes into action.
6 Comments
former_member863634
Discoverer
0 Kudos
Very well done. Very much needed blog to explore and start. Great work. Looking forward to many more such blogs on this. 🙂
neha_singh06
Discoverer
0 Kudos
Thankyou! this will help every developer to understand and work on it. Great Work.
matt
Active Contributor

CALL METHOD is very old fashioned. Nowadays

object1->display( EXPORTING iv_cust_no    = p_kunner
IMPORTING ev_first_name = lv_first_name ... )

is to be preferred. Also, please edit your blog so that the code is in a code block (like I've done above), using the {;} button. This way your examples can be copy pasted.

What you have not done is explain under what circumstances you would want to use an abstract class.

Your code won't work because you haven't instantiated object1. I would use

DATA object1 TYPE REF TO zabstract_7.
...
object1 = NEW zabstract_child_7( ).

This also demonstrates one of the uses of an abstract class - you have multiple implementations of the method, but to use the methods, you don't need to know what the concrete type is.

Finally, you've declared your variables with an L prefix, when in the context they're clearly global. 

Shivani11
Explorer
0 Kudos
Hello Mathew,

Thank you for your response and providing me the correct information. I've changed the mode of code.

In the part of calling object, I've used 'Pattern' so that's why it is showing you in the old-fashioned form.
matt
Active Contributor

You can get code completion of the modern form using Ctrl-Space. Also works in Eclipse.

Another tip: instead of write, use CL_DEMO_OUTPUT. See any blog by Horst Keller for examples!

former_member864155
Discoverer
0 Kudos
Much needed info & Amazing write-up!.
Labels in this area