Abstract Class and Subclass in SAP ABAP
In SAP ABAP, an abstract class is a class that cannot be instantiated on its own and serves as a blueprint for its subclasses. It typically contains abstract methods, which are methods without an implementation in the abstract class. Subclasses should provide strong implementations for these abstract methods.
Sample Code Example:
Let’s consider an example of an abstract class representing different shapes, with an abstract method for calculating the area. Subclasses (e.g., Circle and Square) will provide their specific implementations.
CLASS abstract_shape DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS calculate_area ABSTRACT.
ENDCLASS.
CLASS abstract_shape IMPLEMENTATION.
METHOD calculate_area.
WRITE: 'Area Calculation Not Defined'.
ENDMETHOD.
ENDCLASS.
CLASS circle DEFINITION INHERITING FROM abstract_shape.
PUBLIC SECTION.
METHODS calculate_area REDEFINITION.
ENDCLASS.
CLASS circle IMPLEMENTATION.
METHOD calculate_area.
DATA radius TYPE i VALUE 5.
DATA area TYPE i.
area = COND #( WHEN sy-index = 1 THEN 3.14 * radius * radius
ELSE 0 ).
WRITE: / 'Area of Circle:', area.
ENDMETHOD.
ENDCLASS.
CLASS square DEFINITION INHERITING FROM abstract_shape.
PUBLIC SECTION.
METHODS calculate_area REDEFINITION.
ENDCLASS.
CLASS square IMPLEMENTATION.
METHOD calculate_area.
DATA side_length TYPE i VALUE 4.
DATA area TYPE i.
area = COND #( WHEN sy-index = 1 THEN side_length * side_length
ELSE 0 ).
WRITE: / 'Area of Square:', area.
ENDMETHOD.
ENDCLASS.
Output:
DATA(shape) = NEW abstract_shape( ).
shape->calculate_area( ). " Output: Area Calculation Not Defined
shape = NEW circle( ).
shape->calculate_area( ). " Output: Area of Circle: 78.5
shape = NEW square( ).
shape->calculate_area( ). " Output: Area of Square: 16
Explanation:
- The abstract_shape class has an abstract method calculate_area without an implementation.
- Subclasses circle and square inherit from abstract_shape and provide their own implementations for calculate_area.
- Instantiating an object of type abstract_shape directly is not allowed.
- The calculate_area method dynamically calls the appropriate implementation based on the actual type of the object.
In this example, abstract classes allow for the creation of a common interface while letting subclasses define their specific behavior. The output shows how polymorphism works in the context of an abstract class in SAP ABAP.
SAP ABAP | Polymorphism
Polymorphism in SAP ABAP is a core principle in Object-Oriented Programming (OOP) that enables different classes to be handled as instances of a shared class via a common interface. This allows developers to apply techniques in various ways while storing a constant user interface. In SAP ABAP, the integration of polymorphism is pivotal for augmenting code flexibility and ensuring ease of maintenance.
Table of Content
- What is Polymorphism in SAP ABAP?
- Types of Polymorphism in SAP ABAP:
- Abstract Class and Subclass in SAP ABAP
- Dynamic Type Binding in SAP ABAP
- Conclusion: