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:
- What is Abstract Class?
- Where to use Abstract Class?
- 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?
- As Abstract Class is flexible in nature, so we can use to create multiple flavors of the object with some default operations.
- Abstract Class should be used for those classes which are closely related to each other.
- 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.