Monday, July 13, 2026

πŸš€ SAP ABAP Mock Interview – Question 6/50

 

1️⃣ Which method executes?

Given:

CLASS lcl_vehicle DEFINITION.
  PUBLIC SECTION.
    METHODS start_engine.
ENDCLASS.

CLASS lcl_car DEFINITION
  INHERITING FROM lcl_vehicle.
ENDCLASS.

Notice that start_engine is not redefined in lcl_car.

DATA lo_vehicle TYPE REF TO lcl_vehicle.

CREATE OBJECT lo_vehicle TYPE lcl_car.

lo_vehicle->start_engine( ).

✅ Answer

The method executed is:

lcl_vehicle->start_engine( )

Why?

Although the runtime object is lcl_car, the child class has not provided its own implementation of start_engine.

Since lcl_car inherits the method without redefining it, ABAP uses the inherited implementation from the parent class.

This is the essence of inheritance—the child automatically inherits the parent's behavior unless it explicitly overrides it.


2️⃣ Why does the parent method execute?

Think of inheritance as copying behavior down the hierarchy.

Vehicle
   |
   +---- Car

Since Car doesn't redefine start_engine, it simply uses the inherited version from Vehicle.

If the child later contains:

METHODS start_engine REDEFINITION.

then the child implementation will be executed instead.


3️⃣ Explain Upcasting vs Downcasting

✅ Upcasting (Child → Parent)

DATA lo_vehicle TYPE REF TO lcl_vehicle.
DATA lo_car     TYPE REF TO lcl_car.

CREATE OBJECT lo_car.

lo_vehicle = lo_car.

Characteristics

  • Child → Parent
  • Implicit
  • Safe
  • No CAST operator required
  • Happens automatically

Why?

Because every Car is a Vehicle.


✅ Downcasting (Parent → Child)

DATA lo_vehicle TYPE REF TO lcl_vehicle.
DATA lo_car     TYPE REF TO lcl_car.

lo_car ?= lo_vehicle.

Characteristics

  • Parent → Child
  • Explicit
  • Requires ?=
  • Runtime type check
  • May fail

Why?

Because not every Vehicle is a Car.

A Vehicle could actually be:

  • Car πŸš—
  • Truck 🚚
  • Bike 🏍️
  • Bus 🚌

ABAP therefore verifies the runtime object before allowing the cast.


🎯 What happens during Downcasting?

Suppose:

DATA lo_vehicle TYPE REF TO lcl_vehicle.

CREATE OBJECT lo_vehicle TYPE lcl_car.

DATA lo_car TYPE REF TO lcl_car.

lo_car ?= lo_vehicle.

Successful

Runtime object = lcl_car

Reference after casting:

Vehicle Reference
       |
       V
      Car Object

↓

Car Reference
      |
      V
    Same Car Object


πŸ’‘ Bonus Question

DATA lo_vehicle TYPE REF TO lcl_vehicle.

CREATE OBJECT lo_vehicle TYPE lcl_vehicle.

DATA lo_car TYPE REF TO lcl_car.

lo_car ?= lo_vehicle.

What happens?

❌ Runtime Exception

Reason:

The actual object is a lcl_vehicle, not a lcl_car.

ABAP cannot magically convert a parent object into a child object.

You'll typically get a cast error (for example, CX_SY_MOVE_CAST_ERROR if not handled).


⭐ Senior Interview Insight

A follow-up question is often:

How can we safely downcast without risking a runtime exception?

Use the IS INSTANCE OF operator before casting:

IF lo_vehicle IS INSTANCE OF lcl_car.

  lo_car ?= lo_vehicle.

ENDIF.

Or use exception handling:

TRY.
    lo_car ?= lo_vehicle.
  CATCH cx_sy_move_cast_error.
    " Handle invalid cast
ENDTRY.

This is the recommended approach when the runtime type isn't guaranteed.


🎯 Interview Tip

A question interviewers often ask is:

Why is upcasting implicit, but downcasting explicit?

A strong answer is:

Upcasting is always safe because every child object is also an instance of its parent class. Downcasting is potentially unsafe because a parent reference may point to many different child types—or even a parent object itself. Therefore, ABAP requires an explicit cast and performs a runtime type check before allowing the conversion.


πŸ† Key Takeaways

  • ✅ If a child class does not redefine a method, the inherited parent implementation is executed.
  • Upcasting (Child → Parent) is implicit and always safe.
  • Downcasting (Parent → Child) requires ?= and a runtime type check.
  • ✅ A failed downcast raises a runtime exception unless you guard it with IS INSTANCE OF or handle CX_SY_MOVE_CAST_ERROR.

This is exactly the depth of explanation that distinguishes an experienced ABAP developer in technical interviews.