0% found this document useful (0 votes)
3 views2 pages

Inheritance Example

The document defines a superclass 'lcl_vehicle' with methods for vehicle characteristics and two subclasses: 'lcl_electric_car' and 'lcl_diesel_car' that inherit from it. Each subclass redefines certain methods to provide specific implementations for electric and diesel vehicles, including unique methods for battery and fuel tank capacities. The main program creates instances of both subclasses and demonstrates their functionalities.

Uploaded by

kishancp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Inheritance Example

The document defines a superclass 'lcl_vehicle' with methods for vehicle characteristics and two subclasses: 'lcl_electric_car' and 'lcl_diesel_car' that inherit from it. Each subclass redefines certain methods to provide specific implementations for electric and diesel vehicles, including unique methods for battery and fuel tank capacities. The main program creates instances of both subclasses and demonstrates their functionalities.

Uploaded by

kishancp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

report inheritance.

" Superclass
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: show_engine_type,
number_of_wheels,
refuel,
display_color.
ENDCLASS.

CLASS lcl_vehicle IMPLEMENTATION.


METHOD show_engine_type.
WRITE: / 'Generic engine'.
ENDMETHOD.

METHOD number_of_wheels.
WRITE: / 'Typically 4 wheels'.
ENDMETHOD.

METHOD refuel.
WRITE: / 'Refuel at a station'.
ENDMETHOD.

METHOD display_color.
WRITE: / 'Standard vehicle color: White'.
ENDMETHOD.
ENDCLASS.

" Subclass: Electric Car


CLASS lcl_electric_car DEFINITION INHERITING FROM lcl_vehicle.
PUBLIC SECTION.
METHODS: show_engine_type REDEFINITION,
refuel REDEFINITION,
show_battery_capacity.
ENDCLASS.

CLASS lcl_electric_car IMPLEMENTATION.


METHOD show_engine_type.
WRITE: / 'Electric motor with lithium-ion battery'.
ENDMETHOD.

METHOD refuel.
" Call base class logic first
super->refuel( ).
WRITE: / '→ Connect to Electric Charger'.
ENDMETHOD.

METHOD show_battery_capacity.
WRITE: / 'Battery capacity: 75 kWh'.
ENDMETHOD.
ENDCLASS.

" Subclass: Diesel Car


CLASS lcl_diesel_car DEFINITION INHERITING FROM lcl_vehicle.
PUBLIC SECTION.
METHODS: show_engine_type REDEFINITION,
refuel REDEFINITION,
show_fuel_tank_capacity.
ENDCLASS.
CLASS lcl_diesel_car IMPLEMENTATION.
METHOD show_engine_type.
WRITE: / 'Diesel engine with turbocharger'.
ENDMETHOD.

METHOD refuel.
" Extend base logic with specific behavior
super->refuel( ).
WRITE: / '→ Connect to Fuel Station'.
ENDMETHOD.

METHOD show_fuel_tank_capacity.
WRITE: / 'Fuel tank capacity: 50 liters'.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

DATA: o_electric TYPE REF TO lcl_electric_car,


o_diesel TYPE REF TO lcl_diesel_car.

CREATE OBJECT o_electric.


CREATE OBJECT o_diesel.

WRITE: / 'Electric Car:'.


o_electric->show_engine_type( ).
o_electric->number_of_wheels( ).
o_electric->refuel( ).
o_electric->display_color( ).
o_electric->show_battery_capacity( ).

ULINE.

WRITE: / 'Diesel Car:'.


o_diesel->show_engine_type( ).
o_diesel->number_of_wheels( ).
o_diesel->refuel( ).
o_diesel->display_color( ).
o_diesel->show_fuel_tank_capacity( ).

You might also like