What OOP concepts are demonstrated here?
This example showcases three fundamental Object-Oriented Programming concepts:
✅ Inheritance
CLASS lcl_car DEFINITION INHERITING FROM lcl_vehicle.
The child class (lcl_car) inherits the attributes and methods of the parent class (lcl_vehicle).
✅ Method Overriding (Redefinition)
METHODS start_engine REDEFINITION.
The child class provides its own implementation of the inherited start_engine method while keeping the same method signature.
✅ Polymorphism
The same method call:
lo_vehicle->start_engine( ).
can execute different implementations depending on the runtime object type, not the reference type.
2️⃣ Difference between Overloading and Overriding
| Overloading | Overriding |
|---|---|
| Same method name | Same method name |
| Different parameter list (signature) | Same parameter list (signature) |
| Multiple methods coexist in the same class | Child class replaces parent implementation |
| Decided at compile time | Decided at runtime |
| ❌ Not supported in ABAP | ✅ Fully supported in ABAP |
3️⃣ Does ABAP support Method Overloading?
No.
ABAP Objects does not support method overloading.
The following is not allowed:
METHODS calculate IMPORTING iv_num TYPE i. METHODS calculate IMPORTING iv_text TYPE string.
Method names must be unique within the same class.
How do ABAP developers handle this?
Common alternatives include:
-
Use different method names (
calculate_by_number,calculate_by_text) - Use optional parameters
- Use parameter objects or structures
- Apply design patterns like Factory or Strategy where appropriate
4️⃣ Which method executes?
DATA lo_vehicle TYPE REF TO lcl_vehicle. CREATE OBJECT lo_vehicle TYPE lcl_car. lo_vehicle->start_engine( ).
Answer
The executed method is:
✅ lcl_car->start_engine
Why?
There are two important concepts:
Compile Time
Reference Type:
lcl_vehicle
The compiler only checks whether start_engine exists in lcl_vehicle.
Runtime
Actual Object:
lcl_car
At runtime, ABAP sees that the object is actually a lcl_car.
Since start_engine has been redefined, ABAP invokes the child implementation.
This behavior is known as:
- ✅ Runtime Polymorphism
- ✅ Dynamic Polymorphism
- ✅ Dynamic Method Dispatch (or Dynamic Dispatch)
π― Interview Tip
A common follow-up question is:
Why doesn't ABAP call the parent method, since the reference type is
lcl_vehicle?
A strong answer is:
"The reference type determines which methods are accessible at compile time, while the runtime object type determines which implementation is executed for redefined instance methods."
This distinction demonstrates a solid understanding of polymorphism.
π‘ Bonus Question
What if start_engine is not redefined in lcl_car?
Then:
lo_vehicle->start_engine( ).
will execute:
✅ lcl_vehicle->start_engine
Why?
Since the child class inherits the method but doesn't redefine it, the inherited implementation from the parent class is used.
⭐ Senior-Level Insight
An interviewer may ask:
Can we call the parent implementation from the child class after redefining the method?
Yes.
Use the super reference:
METHOD start_engine. super->start_engine( ). " Child-specific logic ENDMETHOD.
This allows you to reuse the parent's implementation before or after adding child-specific behavior.
π Interview Takeaway
When answering this question, don't stop at:
"The child method is called."
A more complete answer is:
"ABAP uses dynamic polymorphism (dynamic dispatch). The compiler validates the method against the reference type, but at runtime, the system invokes the implementation based on the actual object type. Since the runtime object is
lcl_carand the method is redefined,lcl_car->start_engineis executed."
That level of explanation is what interviewers typically expect from an experienced ABAP developer.
No comments:
Post a Comment