0% found this document useful (0 votes)
3 views1 page

Upcasting Downcating

The document is an ABAP report that demonstrates object-oriented programming concepts, specifically inheritance and method overriding. It defines a generic vehicle class and a car class that inherits from it, showcasing method calls and upcasting. The report also illustrates dynamic method calls and downcasting to access subclass-specific methods.

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 views1 page

Upcasting Downcating

The document is an ABAP report that demonstrates object-oriented programming concepts, specifically inheritance and method overriding. It defines a generic vehicle class and a car class that inherits from it, showcasing method calls and upcasting. The report also illustrates dynamic method calls and downcasting to access subclass-specific methods.

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/ 1

*&---------------------------------------------------------------------*

*& Report ZSAP_TRAINING_INHERITANCE


*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zsap_training_inheritance.
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
METHODS: show_type.
ENDCLASS.

CLASS lcl_vehicle IMPLEMENTATION.


METHOD show_type.
WRITE: / 'I am a generic vehicle'.
ENDMETHOD.
ENDCLASS.

CLASS lcl_car DEFINITION INHERITING FROM lcl_vehicle.


PUBLIC SECTION.
METHODS: open_sunroof.
ENDCLASS.

CLASS lcl_car IMPLEMENTATION.


METHOD open_sunroof.
WRITE: / 'Sunroof opened!'.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

DATA : o_car TYPE REF TO lcl_car.


DATA: o_vehicle TYPE REF TO lcl_vehicle.

CREATE OBJECT o_car.


o_vehicle = o_car. " ✅ Upcasting: allowed automatically

o_vehicle->show_type( ). " ✅ Allowed (inherited method)


*o_vehicle->open_sunroof( ). " ❌ Not allowed – not visible in superclass

" ✅ Dynamic call (if method exists on actual object type)


CALL METHOD o_vehicle->('OPEN_SUNROOF'). " Runtime method lookup

" ✅ Narrow casting to access subclass-specific method


DATA(o_downcast_car) = CAST lcl_car( o_vehicle ).
o_downcast_car->open_sunroof( ).

You might also like