An abstract class is a class that cannot be instantiated directly. It serves as a blueprint for other classes. Abstract classes typically include:
- Abstract methods, which are declared but not implemented.
- Concrete methods, which can be implemented and inherited.
Feature | Description |
---|---|
Not instantiable | Cannot use CREATE OBJECT on abstract classes. |
Used for inheritance | Serve as base classes for child classes. |
Can contain abstract methods | These must be implemented in subclasses. |
Can contain concrete methods | These can be used directly by child classes. |
REPORT zrep_lclclass.
CLASS lcl_animal DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS:
speak ABSTRACT,
move. " This can be implemented if needed
ENDCLASS.
CLASS lcl_animal IMPLEMENTATION.
METHOD move.
WRITE: 'Animal is moving'.
ENDMETHOD.
ENDCLASS.
" Concrete subclass
CLASS lcl_dog DEFINITION INHERITING FROM lcl_animal.
PUBLIC SECTION.
METHODS:
speak REDEFINITION.
ENDCLASS.
CLASS lcl_dog IMPLEMENTATION.
METHOD speak.
WRITE: 'Woof!'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
" Parent Class
DATA(lo_animal) = NEW lcl_animal( ).
lo_animal->speak( ).
WRITE: 'Animal can speak'.
SKIP.
" child class
DATA(lo_dog) = NEW lcl_dog( ).
lo_dog->speak( ).
lo_dog->move( ).
Output:
Check the Program ( CTRL + F2 ).
if not initated the Parent class then the output will be.
Output: