Skip to content

Lesson 8 — Interfaces & Contract-Based Design ​

Inheritance says:

text
Book is a Product

An interface says:

text
This object supports a capability/contract

Objects do not need a common superclass to implement the same interface.

Learning objectives ​

  • create a global ABAP interface;
  • declare interface methods;
  • implement an interface in a class;
  • implement qualified interface methods;
  • declare interface reference variables;
  • assign different implementing objects to one interface reference;
  • build internal tables of interface references;
  • explain interface polymorphism;
  • distinguish inheritance from interface contracts;
  • explain why interfaces reduce coupling.

1. Create an interface ​

Create:

text
ZIF_DESCRIBABLE_###

Source:

abap
INTERFACE zif_describable_### PUBLIC.

  METHODS get_description
    RETURNING VALUE(description) TYPE string.

ENDINTERFACE.

An interface defines a contract, not object state.

2. Implement in Book ​

Add to Book public section:

abap
INTERFACES zif_describable_###.

Implementation:

abap
METHOD zif_describable_###~get_description.
  description = |Book: { title } by { author }|.
ENDMETHOD.

Qualified name:

text
interface~method

3. Implement in Movie ​

Movie implements the same interface with movie-specific output.

Now both classes satisfy the same contract.

4. Interface reference ​

abap
DATA describable TYPE REF TO zif_describable_###.

Assign a Book:

abap
describable = NEW zcl_book_###( ... ).

Call:

abap
out->write( describable->get_description( ) ).

Then assign Movie and call the same method.

5. Interface polymorphism ​

text
REF TO ZIF_DESCRIBABLE
     │
     ├── Book
     ├── Movie
     └── any future implementing class

The caller depends on the capability, not the concrete class hierarchy.

6. Create ZIF_SEARCHABLE_### ​

abap
INTERFACE zif_searchable_### PUBLIC.

  METHODS matches_text
    IMPORTING text TYPE string
    RETURNING VALUE(matches) TYPE abap_bool.

ENDINTERFACE.

Implement it in Book and Movie.

abap
METHOD zif_searchable_###~matches_text.
  DATA(search_text) = to_lower( text ).
  DATA(book_text) = to_lower( |{ title } { author }| ).

  matches = xsdbool( book_text CS search_text ).
ENDMETHOD.

CS checks whether a character-like value contains another.

8. Table of interface references ​

abap
TYPES tt_searchables
  TYPE STANDARD TABLE OF REF TO zif_searchable_###
  WITH EMPTY KEY.

Append Book and Movie objects that implement the interface.

Loop and call matches_text without knowing the concrete class.

9. Interface vs superclass ​

SuperclassInterface
models an is-a familymodels a capability/contract
can contain inherited implementation/statedefines interface components/contracts
single class inheritance chaina class can implement multiple interfaces

Interfaces are especially useful at architectural boundaries because callers can depend on a smaller contract.

10. Prefer contracts at boundaries ​

Instead of:

text
OrderProcessor depends directly on ZCL_EMAIL_SENDER

prefer conceptually:

text
OrderProcessor depends on ZIF_MESSAGE_SENDER

Then multiple implementations can satisfy the contract.

This is a foundation for testability and dependency inversion, even though those topics are not explored deeply here.

11. Summary ​

text
INTERFACE
INTERFACES
interface~method
REF TO interface
interface polymorphism
multiple capabilities
contract-based design

Lesson 9 finishes the ABAP-only module with static members and constants.