ERPClimb logoERPClimb
OOP ABAP
ABAP Developmentintermediate 9 min read

OOP ABAP Part 3: Inheritance, Interfaces and Polymorphism

Preview · a free account unlocks the full lesson

Key takeaway

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 sections

Visual overview

OOP ABAP Part 3: Inheritance, Interfaces and Polymorphism — learning sheetFlow: Run the code example: a table of lif_pricing references with different classes → Add a first-class pricing class — the loop needs no change → Try a wrong down-cast and catch the exception. Key ideas: INHERITANCE; ABSTRACT CLASSES; INTERFACES; POLYMORPHISM. Watch out: Down-casting with ?= fails at runtime with CX_SY_MOVE_CAST_ERROR if the object is not of the target type.ERPClimb.com / ABAP / LESSON 3/10 / LEARNING SHEETOOP ABAP Part 3: Inheritance, Interfaces and Polymorphism1 · HOW IT FLOWS1Run the code examplea table of lif_pricing referenceswith different classes2Add a first-class pricingclass — the loop needs nochange3Try a wrong down-cast andcatch the exception2 · KEY IDEAS EXPLAINEDInheritanceThe subclass inherits public and protectedcomponents (not private).METHODS get_price REDEFINITION — replaces thesuperclass implementation.Abstract ClassesCannot be instantiated; subclasses must implementabstract methods.Useful for a common frame with shared code plusmandatory specific parts.InterfacesA class declares INTERFACES lif_pricing andimplements lif_pricing~get_price.A class can implement many interfaces (the answerto single inheritance).PolymorphismDATA lo_pricing TYPE REF TO lif_pricing.3 · REMEMBER · WATCH OUTRememberInheritance: a subclass (INHERITING FROM) reuses and can REDEFINEmethods of its superclass.Interfaces define a contract; any class implementing it can be usedthrough an interface reference.Polymorphism: the same call (lo_ref->calculate( )) runs differentcode depending on the object's actual class.!Watch outDown-casting with ?= fails atruntime with CX_SY_MOVE_CAST_ERRORif the object is not of the targettype.4 · IN A REAL PROJECTAn output program printed three document types (invoice, credit memo, debit memo) with a large CASE statementrepeated in five places. Adding a new "pro forma" type required changes in all five places and wasmissed twice. The team defined an interface ZIF_DOC_OUTPUT with methods get_form_name and get_texts…Original ERPClimb learning sheet • Verify against your SAP release and edition

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.

Free account

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

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.