Module 12: ABAP Objects (OOP – Core)
Module 12: OOP Advanced Concepts
This module builds on core OOP fundamentals and introduces advanced concepts that enable flexible, extensible, and maintainable ABAP designs.
Most modern SAP frameworks rely heavily on these principles.
1. Inheritance
What is Inheritance?
Inheritance allows a class to reuse and extend behavior of another class.
CLASS zcl_person DEFINITION.
PUBLIC SECTION.
METHODS speak.
ENDCLASS.
CLASS zcl_employee DEFINITION
INHERITING FROM zcl_person.
PUBLIC SECTION.
METHODS speak REDEFINITION.
ENDCLASS.
CLASS zcl_employee IMPLEMENTATION.
METHOD speak.
WRITE 'Employee speaking'.
ENDMETHOD.
ENDCLASS.
ABAP supports single inheritance only.
When to Use Inheritance
Use inheritance for IS-A relationships.
Examples:
-
Employee is a Person
-
SalesOrder is a BusinessObject
Do NOT use inheritance just for code reuse.
2. Interfaces
What is an Interface?
An interface defines what a class can do, without specifying how.
INTERFACE zif_payable.
METHODS calculate_pay
RETURNING VALUE(rv_amount) TYPE p.
ENDINTERFACE.
CLASS zcl_employee DEFINITION.
PUBLIC SECTION.
INTERFACES zif_payable.
ENDCLASS.
CLASS zcl_employee IMPLEMENTATION.
METHOD zif_payable~calculate_pay.
rv_amount = 50000.
ENDMETHOD.
ENDCLASS.
ABAP supports multiple interfaces, enabling flexible design.
Interface vs Inheritance
| Aspect | Interface | Inheritance |
|---|---|---|
| Relationship | CAN-DO | IS-A |
| Multiple | Yes | No |
| Code Reuse | No | Yes |
| Flexibility | High | Medium |
Prefer interfaces over inheritance for extensibility.
3. Polymorphism
What is Polymorphism?
Polymorphism allows different implementations to be accessed through the same interface or superclass reference.
DATA lo_payable TYPE REF TO zif_payable.
lo_payable = NEW zcl_employee( ).
DATA(lv_amount) = lo_payable->calculate_pay( ).
Frameworks like BAdIs, RAP, OData rely heavily on polymorphism.
Runtime Binding
Method calls are resolved at runtime, not compile time.
This is called dynamic binding.
4. Abstract Classes
What is an Abstract Class?
An abstract class:
-
Cannot be instantiated
-
Can contain abstract methods
-
Acts as a base template
CLASS zcl_person DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS speak ABSTRACT.
ENDCLASS.
CLASS zcl_employee DEFINITION
INHERITING FROM zcl_person.
ENDCLASS.
CLASS zcl_employee IMPLEMENTATION.
METHOD speak.
WRITE 'Employee speaks'.
ENDMETHOD.
ENDCLASS.
Abstract classes enforce mandatory behavior in subclasses.
5. Design Principles – SOLID (ABAP View)
5.1 Single Responsibility Principle (SRP)
A class should have one reason to change.
❌ Bad:
CLASS zcl_order DEFINITION.
PUBLIC SECTION.
METHODS save.
METHODS print.
METHODS send_email.
ENDCLASS.
✅ Good:
-
zcl_order_service
-
zcl_order_printer
-
zcl_order_notifier
5.2 Open/Closed Principle (OCP)
Classes should be open for extension, closed for modification.
INTERFACE zif_discount.
METHODS calculate.
ENDINTERFACE.
New discounts → New classes No modification to existing code.
5.3 Liskov Substitution Principle (LSP)
Subclasses must be usable wherever base classes are expected.
Overriding methods that change expected behavior breaks LSP.
5.4 Interface Segregation Principle (ISP)
Many small interfaces are better than one large interface.
❌ Bad:
INTERFACE zif_service.
METHODS save.
METHODS delete.
METHODS print.
ENDINTERFACE.
✅ Good:
-
zif_persistable
-
zif_printable
5.5 Dependency Inversion Principle (DIP)
Depend on abstractions, not concretions.
DATA lo_service TYPE REF TO zif_service.
Not:
DATA lo_service TYPE REF TO zcl_service.
RAP BO handlers rely heavily on DIP.
6. Real SAP Usage Examples
| Concept | SAP Usage |
|---|---|
| Inheritance | Framework base classes |
| Interfaces | BAdIs, RAP |
| Polymorphism | Enhancements, extensibility |
| Abstract Classes | Business object templates |
| SOLID | Clean ABAP / Testable code |
7. Common Mistakes
-
Deep inheritance hierarchies
-
Fat interfaces
-
Hard-coded class references
-
Ignoring SOLID principles
-
Using inheritance instead of composition
8. Summary
-
Inheritance models IS-A relationships
-
Interfaces define capabilities
-
Polymorphism enables flexibility
-
Abstract classes enforce contracts
-
SOLID principles guide maintainable ABAP design
9. Practice Exercises
-
Create an abstract base class with mandatory methods.
-
Implement multiple interfaces in a class.
-
Demonstrate polymorphism using interface references.
-
Refactor a class violating SRP.
-
Apply DIP using interfaces.
10. What’s Next?
➡️ Module 13: ALV Reporting
If you understand this module, framework-based ABAP becomes easy.