OOP ABAP Part 3: Inheritance, Interfaces and Polymorphism
Preview · a free account unlocks the full lesson
Learn inheritance (INHERITING FROM, REDEFINITION, super- ), interfaces (INTERFACES, alias access), polymorphism through superclass and interface references, and up/down casts with ? = and CX SY MOVE CAST ERROR.
In this lessonShow sectionsHide sections
Visual overview
Explanation
Parts 1 and 2 of this series covered single classes in isolation; now classes start working together through inheritance and interfaces. Inheritance lets a subclass, declared with INHERITING FROM, reuse its superclass's methods and REDEFINE them where needed; interfaces define a contract that any implementing class can be used through, via an interface reference; and polymorphism means the same call, such as lo_ref->calculate( ), runs different code depending on the actual class of the object behind the reference.
With CLASS lcl_cargo_flight DEFINITION INHERITING FROM lcl_flight., the subclass inherits its superclass's public and protected components, though never its private ones. METHODS get_price REDEFINITION replaces the superclass's implementation for that method, and inside the redefinition, super->get_price( ) calls the original superclass version if needed. ABAP only supports single inheritance, meaning a class can have exactly one superclass; a FINAL class cannot be inherited from at all, and a FINAL method cannot be redefined.
An abstract class, declared with CLASS lcl_ticket DEFINITION ABSTRACT and containing METHODS get_price ABSTRACT., cannot be instantiated directly, and any subclass of it must implement its abstract methods — useful for giving a family of classes a common frame with shared code plus parts that each subclass must supply for itself.
Create a free account to keep reading
You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.
- Full explanation
- Code example
- Real project scenario
- Common mistakes
- Best practices
- Interview angle
One tap with Google works too. Every topic keeps its first lesson open to everyone.
Keep exploring
Interview questions on this topic
Related SAP topics
Referenced SAP objects
Reviewed reference pages for the objects this page mentions.
Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.