Abstract Class
- Defined with
CLASS ... DEFINITION ABSTRACT
. - Can have:
- Attributes (data)
- Concrete methods (implemented)
- Abstract methods (declared but not implemented — must be redefined in subclass)
- Cannot be instantiated directly — only subclasses can be created.
- Supports inheritance (a subclass inherits implementation + attributes).
- Can implement interfaces too.
Think of an abstract class as a partially implemented blueprint.
Example :
CLASS cl_vehicle DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS: start ABSTRACT, ” must be implemented in subclass
stop. ” already implemented
ENDCLASS.
CLASS cl_vehicle IMPLEMENTATION.
METHOD stop.
WRITE: / ‘Vehicle stopped.’.
ENDMETHOD.
ENDCLASS.
Interface
- Defined with
INTERFACE ...
. - Can contain:
- Method declarations only (no implementation).
- Constants, types, attributes (but no data storage).
- No concrete code inside (implementation happens in the class that implements it).
- A class can implement multiple interfaces (like multiple inheritance).
- Cannot be instantiated or inherited — only implemented.
Think of an interface as a contract: “any class implementing me must provide these methods.”
Example:
INTERFACE if_flyable.
METHODS fly.
ENDINTERFACE.
INTERFACE if_swimmable.
METHODS swim.
ENDINTERFACE.
Key Differences:
Feature | Abstract Class | Interface |
---|---|---|
Instantiation | Cannot be instantiated | Cannot be instantiated |
Inheritance | Single inheritance only | Multiple interfaces can be implemented |
Implementation | Can have abstract + concrete methods | Only method signatures, no code |
Attributes | Yes (instance & static data possible) | Only constants/types, no data |
Purpose | Provide a base class with shared logic | Define a contract for behavior |
When to Use
-
Use an abstract class when:
-
You want to share implementation across subclasses.
-
You want to define default behavior that can be reused.
-
Example:
CL_VEHICLE
(base) →CL_CAR
,CL_TRUCK
.
-
-
Use an interface when:
-
You just need a common contract across unrelated classes.
-
You want to achieve multiple inheritance of behavior.
-
Example:
IF_FLYABLE
,IF_SWIMMABLE
,IF_SERIALIZABLE
.
-