Bird
Raised Fist0

Given a class hierarchy where class D inherits from B and C, and both B and C inherit from A, which line in the following pseudocode is the subtle bug causing the Diamond Problem to manifest incorrectly?

medium🐞 Bug Identification Q7 of Q15
OOP & Design Patterns - Inheritance - Types, Method Resolution Order & Diamond Problem
Given a class hierarchy where class D inherits from B and C, and both B and C inherit from A, which line in the following pseudocode is the subtle bug causing the Diamond Problem to manifest incorrectly? 1: class A { method foo() } 2: class B extends A { override foo() } 3: class C extends A { override foo() } 4: class D extends B, C { no override }
ALine 1: A's foo() is not marked virtual/overridable
BLine 2: B overrides foo() without calling super.foo()
CLine 3: C overrides foo() without calling super.foo()
DLine 4: D does not override foo(), causing ambiguity
Step-by-Step Solution
Solution:
  1. Step 1: Identify diamond problem cause

    Diamond problem arises when subclass inherits multiple conflicting overrides without resolution.
  2. Step 2: Analyze lines

    Lines 2 and 3 override foo(), but line 4 does not override or resolve ambiguity, causing the problem.
  3. Step 3: Other lines

    Line 1 is base method, virtualness depends on language; lines 2 and 3 overriding is normal.
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    D must override to resolve ambiguity [OK]
Quick Trick: Subclass must override to resolve diamond ambiguity [OK]
Common Mistakes:
MISTAKES
  • Blaming base class for diamond problem
  • Thinking overrides without super calls cause diamond
  • Assuming no override is safe
Trap Explanation:
PITFALL
  • Candidates often miss that subclass must explicitly resolve conflicting inherited methods.
Interviewer Note:
CONTEXT
  • Tests ability to spot subtle inheritance bugs causing diamond issues.
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