Bird
Raised Fist0

In a diamond inheritance pattern where class F inherits from classes D and E, both of which inherit from class B, what is the expected behavior when a method defined in B is called on an instance of F in a language with proper MRO?

medium📖 Theory Q4 of Q15
OOP & Design Patterns - Inheritance - Types, Method Resolution Order & Diamond Problem
In a diamond inheritance pattern where class F inherits from classes D and E, both of which inherit from class B, what is the expected behavior when a method defined in B is called on an instance of F in a language with proper MRO?
AAn error occurs due to ambiguous method resolution
BThe method in <code>B</code> is called twice, once via <code>D</code> and once via <code>E</code>
CThe method in <code>B</code> is called only once, following the MRO to avoid duplication
DThe method in <code>F</code> overrides and prevents calling <code>B</code>'s method
Step-by-Step Solution
Solution:
  1. Step 1: Recognize diamond inheritance

    Class F inherits from D and E, both inheriting from B, forming a diamond.
  2. Step 2: Understand MRO behavior

    Proper MRO linearizes the hierarchy to ensure B's method is called only once.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Diamond problem resolved by single call via MRO [OK]
Quick Trick: MRO calls shared base method once in diamond [OK]
Common Mistakes:
MISTAKES
  • Thinking the base method is called multiple times
  • Assuming ambiguity causes errors
  • Ignoring MRO linearization
Trap Explanation:
PITFALL
  • Option B seems logical but ignores MRO's linearization; Option C is incorrect as MRO resolves ambiguity; Option D is unrelated unless F overrides.
Interviewer Note:
CONTEXT
  • Evaluates knowledge of diamond inheritance resolution using MRO.
Master "Inheritance - Types, Method Resolution Order & Diamond Problem" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes