1.Global Class:
- All the features we saw in the previous post regarding local class and components of class, objects are applicable to global class as well.
- However, unlike local classes, a global class is defined at the global level and can be accessed across various programs, function groups, or function modules within the SAP system.
- Global classes can be created using SE24 (ABAP Workbench).
- The naming convention for global classes is ZCL<class_name>.
- It is possible to convert a local class into a global class, but the reverse — converting a global class into a local class — is not allowed.
SE24 → Object Type → Import → Local Classes in the Program. Enter the program name → preferred global class name. Click Import, then Save the class and Activate it.
Global Class Example:
Report Program:
REPORT yak_global_class_pgm.
INITIALIZATION.
PARAMETERs p_cust TYPE kna1-kunnr.
DATA: lv_name1 TYPE kna1-name1,
lv_ort01 TYPE kna1-ort01.
START-OF-SELECTION.
DATA: obj TYPE REF TO yak_global.
CREATE OBJECT obj.
CALL METHOD obj->m1
EXPORTING
kunnr = p_cust
IMPORTING
name1 = lv_name1
ort01 = lv_ort01.
WRITE: lv_name1, lv_ort01.
2.How to call a method inside a method and "ME" Keyword.
- Below code demonstrates the concept of calling a method within another method in ABAP Object-Oriented Programming (OOP) and how a public method can access protected attributes and protected methods.
- The
ME
keyword represents the current calling object.
*&-------------------------------------------------------------------
*& Report YAK_OOP_METHOD
*&-------------------------------------------------------------------
REPORT yak_oop_method.
CLASS lcl_demo_method DEFINITION.
PUBLIC SECTION.
METHODS set IMPORTING empid TYPE i
empname TYPE c OPTIONAL.
PROTECTED SECTION.
DATA: lv_empid TYPE i,
lv_empname(20) TYPE c.
METHODS: display.
ENDCLASS.
CLASS lcl_demo_method IMPLEMENTATION.
METHOD set.
lv_empid = empid. " Assigning parameter to protected attribute
lv_empname = empname." Assigning parameter to protected attribute
CALL METHOD me->display. " Calling the protected method 'display'
ENDMETHOD.
METHOD display.
WRITE: / 'Employee ID:', lv_empid,
/ 'Employee Name:', lv_empname.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: obj TYPE REF TO lcl_demo_method.
CREATE OBJECT obj. " Creating object of lcl_demo_method
CALL METHOD obj->set " Calling 'set' method with employee details
EXPORTING
empid = 100456
empname = 'SAP DEV'.
3.Structure and Internal Tables in Class
Requirement: object-oriented approach to retrieving and displaying customer data based on a specified country (land1
).
*&-------------------------------------------------------------------
*& Report YAK_OOP_METHOD1
*&-------------------------------------------------------------------
REPORT yak_oop_method1.
CLASS lcl_customers DEFINITION.
PUBLIC SECTION.
METHODS get_customer IMPORTING land1 TYPE land1.
PROTECTED SECTION.
TYPES: BEGIN OF ty_cust,
kunnr TYPE kna1-kunnr,
land1 TYPE land1,
name1 TYPE name1,
ort01 TYPE ort01,
END OF ty_cust.
DATA: it_cust TYPE STANDARD TABLE OF ty_cust,
wa_cust TYPE ty_cust.
METHODS display.
ENDCLASS.
CLASS lcl_customers IMPLEMENTATION.
METHOD get_customer.
SELECT kunnr, land1, name1, ort01
FROM kna1
INTO TABLE @it_cust
WHERE land1 = @land1.
IF sy-subrc EQ 0.
CALL METHOD me->display.
ENDIF.
ENDMETHOD.
METHOD display.
LOOP AT it_cust INTO wa_cust.
WRITE:/ wa_cust-kunnr,
wa_cust-land1,
wa_cust-name1,
wa_cust-ort01.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
PARAMETERS p_land1 TYPE kna1-land1.
DATA: ob TYPE REF TO lcl_customers.
CREATE OBJECT ob.
CALL METHOD ob->get_customer
EXPORTING
land1 = p_land1.
4. FRIEND Class, DEFFER Keyword:
Example:
*&-------------------------------------------------------------------
*& Report YAK_OOP_FRIEND
*&-------------------------------------------------------------------
REPORT yak_oop_friend.
" Declare class lcl_friend later in the program using DEFERRED
CLASS lcl_friend DEFINITION DEFERRED.
" Declare class lcl_class and specify that lcl_friend is a FRIEND class
CLASS lcl_class DEFINITION FRIENDS lcl_friend.
PUBLIC SECTION.
METHODS m1. " Public method
PROTECTED SECTION.
METHODS m2. " Protected method
PRIVATE SECTION.
METHODS m3. " Private method
ENDCLASS.
" Implement methods of class lcl_class
CLASS lcl_class IMPLEMENTATION.
METHOD m1.
WRITE:/ 'Public Section Method m1'.
ENDMETHOD.
METHOD m2.
WRITE:/ 'Protected Section Method m2'.
ENDMETHOD.
METHOD m3.
WRITE:/ 'Private section method m3'.
ENDMETHOD.
ENDCLASS.
" Define class lcl_friend and implement its method
CLASS lcl_friend DEFINITION.
PUBLIC SECTION.
METHODS mf. " Friend class method
ENDCLASS.
CLASS lcl_friend IMPLEMENTATION.
METHOD mf.
WRITE:/ 'Friend class method'.
" Create an instance of lcl_class
DATA: lcl_obj TYPE REF TO lcl_class.
CREATE OBJECT lcl_obj.
" Call methods of lcl_class
CALL METHOD lcl_obj->m1. " Public method
CALL METHOD lcl_obj->m2. " Protected method (accessible due to FRIEND relationship)
CALL METHOD lcl_obj->m3. " Private method (accessible due to FRIEND relationship)
ENDMETHOD.
ENDCLASS.
" Main program execution
START-OF-SELECTION.
" Create an instance of the friend class lcl_friend
DATA: f_obj TYPE REF TO lcl_friend.
CREATE OBJECT f_obj.
" Call the method of the friend class
CALL METHOD f_obj->mf.
5.Inheritance:
- In Object-Oriented Programming (OOP), inheritance allows one class to access the components (methods and attributes) of another class. It enables the reuse and extension of functionality from one class into another.
The class from which components are inherited is called the superclass or base class, while the class that inherits the components is referred to as the subclass or derived class.
- Inheritance in OOP supports the inheritance of public and protected components of the base class. However, private components of the base class cannot be inherited by a subclass.
- In ABAP, the
INHERITING FROM
keyword is used as part of the class definition in the subclass to achieve inheritance. REDEFINE
keyword is used to override or redefine a method of a parent class in a subclass.This allows you to modify or extend the behavior of the method in the subclass.- SUPER Keyword used to call the parent class method implementation in sub class.
- Final is the keyword using which we can restrict the class to be inherited.ie.. if the class has final then it cannot be inherited.
Types of Inheritance:
- Single Inheritance: A subclass inherits from a single superclass.
- Multiple Inheritance: A subclass inherits from more than one superclass.
- Multilevel Inheritance: A subclass inherits from a superclass, and that superclass itself is derived from another class.
- Single Inheritance: In the example, the
lcl_cycle
class inherits from thelcl_vehicle
class. This is an example of single inheritance, - Multilevel Inheritance:The
lcl_car
class is an example of multilevel inheritance, wherelcl_car
inherits fromlcl_cycle
, andlcl_cycle
itself inherits fromlcl_vehicle
. This creates a chain of inheritance, wherelcl_car
indirectly inherits fromlcl_vehicle
throughlcl_cycle
.
lcl_vehicle
: This is the base class (parent class) with properties likewheels
andcolor
, and a methoddisplay
.lcl_cycle
: This is a subclass oflcl_vehicle
and adds additional functionality, likesetcycle
and thebreak
property.lcl_car
: This is a subclass oflcl_cycle
and overrides thedisplay
method while adding additional functionality, such as thegears
property.
*&-------------------------------------------------------------------
*& Report YAK_OOP_INHERITANCE
*&-------------------------------------------------------------------
REPORT yak_oop_inheritance.
" Base class for vehicles
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS display.
PROTECTED SECTION.
DATA: wheels TYPE i,
color(10) TYPE c. " Parameters
ENDCLASS.
CLASS lcl_vehicle IMPLEMENTATION.
METHOD display.
WRITE: wheels, color. " Display wheels and color of the vehicle
ENDMETHOD.
ENDCLASS.
" Derived class for cycles inheriting from lcl_vehicle
CLASS lcl_cycle DEFINITION INHERITING FROM lcl_vehicle.
PUBLIC SECTION.
METHODS setcycle.
PROTECTED SECTION.
DATA: break TYPE i. " Property for break count in cycles
ENDCLASS.
CLASS lcl_cycle IMPLEMENTATION.
METHOD setcycle.
wheels = 2. " Cycles have 2 wheels
color = 'RED'. " Cycles are RED by default
break = 2. " Cycles have 2 breaks
ENDMETHOD.
ENDCLASS.
" Derived class for cars inheriting from lcl_cycle
CLASS lcl_car DEFINITION INHERITING FROM lcl_cycle.
PUBLIC SECTION.
METHODS setcar.
METHODS display REDEFINITION. " Method override to change the display output
PROTECTED SECTION.
DATA: gears TYPE i. " Property for gears in the car
ENDCLASS.
CLASS lcl_car IMPLEMENTATION.
METHOD setcar.
wheels = 4. " Cars have 4 wheels
color = 'SILVER'. " Cars are SILVER by default
break = 1. " Cars have 1 break (for simplicity in this example)
gears = 5. " Cars have 5 gears
ENDMETHOD.
METHOD display.
CALL METHOD super->display. "Calling Parent class Implementation
WRITE: / break, gears. " Override display to include gears information
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
" Create an object for lcl_cycle and call methods
DATA: ob TYPE REF TO lcl_cycle.
CREATE OBJECT ob.
CALL METHOD ob->setcycle.
CALL METHOD ob->display.
" Create an object for lcl_car and call methods
DATA: ob_car TYPE REF TO lcl_car.
CREATE OBJECT ob_car.
CALL METHOD ob_car->setcar.
CALL METHOD ob_car->display.
In ABAP, you can use the FINAL
keyword to restrict a class from being inherited. When a class is declared as FINAL
, it cannot be further subclassed, meaning no class can inherit from it.
Here’s how the FINAL
keyword works:
FINAL
class:FINAL
class:In the above case, you would get an error at compile time saying:
- "The class
lcl_final_class
is declared as FINAL, so it cannot be inherited."
-
In ABAP, the
REDEFINE
Keyword:REDEFINE
keyword is used in the subclass to override a method inherited from the superclass. This allows the subclass to provide its own implementation of the method. -
Visibility/Method Type:When you override a method in a subclass, the visibility (such as
PUBLIC
,PROTECTED
, orPRIVATE
) and method type (such as instance method or static method) of the parent class method cannot be changed.- Only
PUBLIC
andPROTECTED
methods from the superclass can be redefined in the subclass. - Private methods cannot be redefined.
- Only
-
Using theIf you want to call the method of the superclass from within the redefined method of the subclass, you use the
SUPER
Keyword:SUPER
keyword. This allows you to call the original method implementation from the parent class in addition to the new functionality defined in the subclass. -
Static Methods:Static methods (methods defined with the
CLASS-METHODS
keyword) cannot be redefined in the subclass. Only instance methods (methods defined withMETHODS
) can be overridden.
8. Abstract Class in ABAP
- A class that contains at least one abstract method is called an abstract class. An abstract method is a method that is declared but not implemented in the class.
- We declare a method as abstract when we do not know its implementation details yet. The actual implementation of the abstract method is provided in the subclass.
- Abstract methods are declared using the ABSTRACT keyword. If a class contains at least one abstract method, the class itself must also be declared as abstract.
- Abstract methods should be declared in the public or protected section of the class. They are also referred to as non-concrete methods.
- Static methods and constructors cannot be declared as abstract because they cannot be redefined in a subclass.
- Any subclass that inherits from an abstract class must implement all of the abstract methods of the superclass. If a subclass does not implement all abstract methods, it too must be declared as abstract.
- Abstract classes cannot be instantiated directly because they are incomplete. However, once the abstract methods are implemented in the subclass, we can create an object of the subclass.
- Assigning a subclass object to a reference of the abstract superclass is known as narrow casting. For example,
<Abs_spr_cls_ref> = <Obj_sub_cls>
. - Narrow casting refers to the process of assigning a more specific object (subclass) to a reference of a more general type (abstract superclass), essentially switching from a more detailed view to a less detailed one
- When narrow casting is done, the reference variable can access only the methods of the superclass. To access methods specific to the subclass, we need to use dynamic method calls. CALL METHOD <Abs_spr_cls_ref>->('<sub_cls_name>').
Syntax Example:
*&------------------------------------------------------------------
*& Report YAK_OOP_ABSTRACT
*&------------------------------------------------------------------
REPORT yak_oop_abstract.
CLASS lcl_restaurant DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS : store,
display,
payment ABSTRACT.
PROTECTED SECTION.
DATA : tableno TYPE i,
steward TYPE string.
ENDCLASS.
CLASS lcl_restaurant IMPLEMENTATION.
METHOD store.
tableno = 3.
steward = 'ABC'.
ENDMETHOD.
METHOD display.
WRITE :/ tableno, steward.
ENDMETHOD.
ENDCLASS.
CLASS lcl_cheque DEFINITION INHERITING FROM lcl_restaurant.
PUBLIC SECTION.
METHODS payment REDEFINITION.
METHODS m1.
PROTECTED SECTION.
DATA : cqno TYPE i,
cqdate TYPE d,
cqamt TYPE i.
ENDCLASS.
CLASS lcl_cheque IMPLEMENTATION.
METHOD payment.
cqno = 123.
cqdate = sy-datum.
cqamt = 455.
WRITE :/ 'Details of Cheque:', cqno, cqdate, cqamt.
ENDMETHOD.
METHOD m1.
WRITE :/ 'Inside direct method m1 of cheque class'.
ENDMETHOD.
ENDCLASS.
CLASS lcl_creditcard DEFINITION INHERITING FROM lcl_restaurant.
PUBLIC SECTION.
METHODS payment REDEFINITION.
PROTECTED SECTION.
DATA : ccno TYPE i,
ccexpdate TYPE d,
ccamt TYPE i.
ENDCLASS.
CLASS lcl_creditcard IMPLEMENTATION.
METHOD payment.
ccno = 455.
ccexpdate = sy-datum.
ccamt = 234.
WRITE :/ 'Credit card details:', ccno, ccexpdate, ccamt.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA r TYPE REF TO lcl_restaurant.
* Create object r. "syntax error
DATA cq TYPE REF TO lcl_cheque.
CREATE OBJECT cq.
FORMAT COLOR 3.
WRITE :/ 'Using Cheque class object'.
CALL METHOD : cq->store,
cq->display,
cq->payment,
cq->m1.
r = cq. "Narrow casting
FORMAT COLOR 7.
WRITE :/ 'Cheque class object assigned to restaurant'.
CALL METHOD : r->store,
r->display,
r->payment.
* r->m1. "syntax error
" Dynamic method call to access m1 method of the subclass
CALL METHOD r->('M1').
ULINE.
FORMAT COLOR 1.
WRITE :/ 'CREDIT CARD Object....'.
DATA cc TYPE REF TO lcl_creditcard.
CREATE OBJECT cc.
CALL METHOD : cc->store,
cc->display,
cc->payment.
r = cc. "Narrow casting
FORMAT COLOR 2.
WRITE :/ 'Credit card class object assigned to restaurant'.
CALL METHOD : r->store,
r->display,
r->payment.
Pure Abstract Class:
An interface in ABAP is often referred to as a "pure abstract class." This means that all methods defined within an interface are abstract by default. Interfaces do not contain any method implementations, only method declarations.-
Public Visibility:
Interface components, such as constants and methods, are public by default. An interface does not have an explicit visibility section like a normal class. Therefore, any method or constant declared in an interface is automatically public and accessible to classes that implement the interface. -
No Method Implementations:
Interfaces cannot contain method implementations. They only contain method declarations. The responsibility for providing the method implementation lies with the class that implements the interface. -
Implementing an Interface:
If a class wants to implement an interface (whether local or global), it must declare the interface in the public section of the class using theINTERFACES
keyword.
The class must also implement all the methods declared in the interface.- If the class does not implement all the methods, those methods should be flagged as abstract and the class should be considered an abstract class. Otherwise system will throw syntax error.
- The abstract class must then be inherited by a subclass, which must redefine and provide implementations for the abstract methods. So that we can access those methods using subclass objects, otherwise we cant. Pls refer the example 2 for more clarity.
- If the class does not implement all the methods, those methods should be flagged as abstract and the class should be considered an abstract class. Otherwise system will throw syntax error.
-
Implementation Class:
A class that implements an interface is called the implementation class. This class provides the concrete implementations of the all the methods declared in the interface. -
Interfaces must in the Public Section:
When implementing an interface, the interface must be declared in the public section of the local class. This ensures that the methods of the interface are accessible from outside the class. -
Multiple Inheritance:
Interfaces allow for multiple inheritance. A single class can implement multiple interfaces, which means it can inherit method declarations from multiple sources. This is one of the main advantages of using interfaces in object-oriented programming. -
Prefixing Interface Methods:
When referring to a method of an interface outside the context of the interface, the method must be prefixed with the name of the interface, followed by a tilde (~
).<interface_name>~<method_name>
This ensures that you are calling the method of the correct interface.
- Aliases Name for Interface component:
Aliases are alternative names for interface components, improving readability and reducing the need for long naming conventions. -
No Constructors in Interfaces :
Interfaces cannot contain constructors. Constructors are specific to classes, and since interfaces do not contain any implementation, they do not define any constructor logic. Interface Inheritance:
- In ABAP, an interface can include other interfaces. When a class implements an interface that has included interfaces, the class must implement all the methods from the parent interface as well as any methods from the included interfaces.
- This means that if an interface inherits methods from other interfaces (whether directly or indirectly), the class implementing the main interface is required to implement all methods from all inherited interfaces, not just those defined in the immediate interface.
Global Classes Interfaces:
If a global class needs to implement one or more global interfaces, it must declare those interfaces in theINTERFACES tab.
The Below ABAP code you demonstrates the concept of interfaces, multiple inheritance, and narrow casting. Here, I have implemented a local interface and a local implementation class. This can also be achieved using a global interface, a global implementation class, and a driver program.
- Interfaces:
if_rectangle
andif_square
are interfaces that define common methods (area
andperimeter
) and constants (such aslength
,breadth
, andside
). - Multiple Inheritance: The class
lcl_if
implements both interfacesif_rectangle
andif_square
. - Narrow Casting: An object of class
lcl_if
is cast into references of the interfacesif_rectangle
andif_square
to demonstrate how the methods of the interfaces can be called from the interface references.
*&----------------------------------------------------------------
*& Report YAK_OOP_INTERFACE
*&----------------------------------------------------------------
REPORT yak_oop_interface.
INTERFACE if_rectangle.
" In Interface By Default, all components are public
CONSTANTS: length TYPE i VALUE 10,
breadth TYPE i VALUE 5.
METHODS: area,
perimeter.
ENDINTERFACE.
INTERFACE if_square.
CONSTANTS: side TYPE i VALUE 10.
METHODS: area,
perimeter.
ENDINTERFACE.
CLASS lcl_if DEFINITION.
PUBLIC SECTION.
INTERFACES: if_rectangle,
if_square.
PROTECTED SECTION.
DATA: res TYPE i.
ENDCLASS.
CLASS lcl_if IMPLEMENTATION.
METHOD if_rectangle~area.
WRITE: / 'This is from the interface rectangle - area method'.
ENDMETHOD.
METHOD if_rectangle~perimeter.
WRITE: / 'This is from the interface rectangle - perimeter method'.
ENDMETHOD.
METHOD if_square~area.
WRITE: / 'This is from the interface square - area method'.
ENDMETHOD.
METHOD if_square~perimeter.
WRITE: / 'This is from the interface square - perimeter method'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: ob TYPE REF TO lcl_if.
CREATE OBJECT ob.
NEW-LINE.
WRITE: / 'From class object'.
CALL METHOD: ob->if_rectangle~area,
ob->if_rectangle~perimeter,
ob->if_square~area,
ob->if_square~perimeter.
DATA: if_ref_rec TYPE REF TO if_rectangle.
DATA: if_ref_sq TYPE REF TO if_square.
" Narrow Casting: Assigning object reference to interface reference
if_ref_rec = ob. " Narrow Casting for if_rectangle
NEW-LINE.
WRITE: / 'From interface rectangle reference'.
CALL METHOD: if_ref_rec->area,
if_ref_rec->perimeter.
if_ref_sq = ob. " Narrow Casting for if_square
NEW-LINE.
WRITE: / 'From interface square reference'.
CALL METHOD: if_ref_sq->area,
if_ref_sq->perimeter.
Output Example:
Interface Definition:
- The interface
if_part_impl
defines three methods:m1
,m2
, andm3
.
- The interface
-
Abstract Class
lcl_part_impl
:- The class
lcl_part_impl
is declared as abstract and partially implements the interface. - Only the
m1
method is implemented inlcl_part_impl
, and methodsm2
andm3
are marked as abstract in the class definition (ABSTRACT METHODS m2 m3
).
- The class
-
Subclass Implementation
lcl_sub_class
:- The subclass
lcl_sub_class
inherits fromlcl_part_impl
and provides implementations for the abstract methodsm2
andm3
that were left unimplemented in the parent class. - The methods
m2
andm3
are redefined in the subclass using theREDEFINITION
keyword.
- The subclass
-
Main Program:
- An instance of the subclass
lcl_sub_class
is created, and the interface methods (m1
,m2
, andm3
) are called. - The
m1
method is executed from the abstract class, whilem2
andm3
are executed from the subclass.
- An instance of the subclass
*&-----------------------------------------------------------------
*& Report YAK_OOP_INTERFACE_PART_IMPL
*&-----------------------------------------------------------------
REPORT yak_oop_interface_part_impl.
INTERFACE if_part_impl.
METHODS: m1,
m2,
m3.
ENDINTERFACE.
"Class partially Implements Interface Methods
CLASS lcl_part_impl DEFINITION ABSTRACT.
PUBLIC SECTION.
INTERFACES: if_part_impl ABSTRACT METHODS m2 m3.
ENDCLASS.
CLASS lcl_part_impl IMPLEMENTATION.
METHOD if_part_impl~m1.
WRITE: / 'Implementing interface method m1 by patial impl class'.
ENDMETHOD.
ENDCLASS.
*START-OF-SELECTION.
"Instances of the abstract class "LCL_PART_IMPL" cannot be generated. -
*data: ob_part_impl TYPE REF TO lcl_part_impl.
*create OBJECT ob_part_impl.
CLASS lcl_sub_class DEFINITION INHERITING FROM lcl_part_impl.
PUBLIC SECTION.
METHODS: if_part_impl~m2 REDEFINITION,
if_part_impl~m3 REDEFINITION.
ENDCLASS.
CLASS lcl_sub_class IMPLEMENTATION.
METHOD if_part_impl~m2.
WRITE:/ 'Implementing interface method m1 by subclass'.
ENDMETHOD.
METHOD if_part_impl~m3.
WRITE:/ 'Implementing interface method m2 by subclass'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: ob_sub_class TYPE REF TO lcl_sub_class.
CREATE OBJECT ob_sub_class.
"Calling methods using sub class obj
CALL METHOD: ob_sub_class->if_part_impl~m1,
ob_sub_class->if_part_impl~m2,
ob_sub_class->if_part_impl~m3.
Example3 - Aliases Name for Interface component:
Aliases are alternative names for interface components, improving readability and reducing the need for long naming conventions.
No comments:
Post a Comment