Java Inheritance - Challenging Exam Problems
Test your understanding of all inheritance concepts with tricky solutions
Problem 1: Constructor Chaining & Super
• Difficulty: MEDIUM
• Concepts: Constructor order, super(), inheritance chain
What is the output?
class A { A() { [Link]("A Constructor"); } }
class B extends A { B() { [Link]("B Constructor");
} } class C extends B { C() { [Link]("C
Constructor"); } } public class Test { public static void
main(String[] args) { C obj = new C(); } }
A) C Constructor
B) B Constructor
C) C Constructor, B Constructor, A Constructor
D) A Constructor, B Constructor, C Constructor
Answer: D
Explanation: Constructors execute from superclass to subclass (top-down). Even without
explicit super() calls, the default constructor of each superclass is automatically invoked first.
Problem 2: Method Overriding vs Overloading
• Difficulty: MEDIUM
• Concepts: Polymorphism, method signatures, overriding
What is the output?
class Parent { void display()
{ [Link]("Parent"); } void display(String s) {
[Link]("Parent: " + s); } } class Child extends Parent
{ void display() { [Link]("Child"); } void
display(int x) { [Link]("Child: " + x); } } public
class Test { public static void main(String[] args) { Child c =
new Child(); [Link](); [Link]("Hello");
[Link](42); } }
A) Child / Parent: Hello / Child: 42
B) Parent / Parent: Hello / Parent: 42
C) Child / Child / Child
D) Compilation Error
Answer: A
Explanation: display() overrides parent. display(String) calls parent's overloaded version.
display(int) calls child's overload. Overloading resolved at compile-time; overriding at runtime.
Problem 3: Superclass Reference & Type Casting
• Difficulty: HARD
• Concepts: Dynamic dispatch, reference vs object, accessibility
What is the output?
class Vehicle { void start() { [Link]("Vehicle
Starting"); } } class Car extends Vehicle { void start()
{ [Link]("Car Starting"); } void openTrunk() {
[Link]("Trunk Opened"); } } public class Test { public
static void main(String[] args) { Vehicle v = new Car();
[Link](); [Link](); } }
A) Car Starting / Trunk Opened
B) Vehicle Starting / Compilation Error
C) Car Starting / Compilation Error
D) Runtime Exception
Answer: C
Explanation: [Link]() calls overridden Car method. But openTrunk() is NOT in Vehicle class, so
it's inaccessible through Vehicle reference (even though actual object is Car).
Problem 4: Protected Members & Visibility
• Difficulty: HARD
• Concepts: Access modifiers, protected visibility, inheritance
Which lines compile successfully?
class Animal { private int age = 5; protected int weight = 10;
public int height = 20; } class Dog extends Animal { void display() {
[Link](age); // Line 1
[Link](weight); // Line 2
[Link](height); // Line 3 } }
A) All three lines compile
B) Only Line 2 and 3 compile
C) Only Line 3 compiles
D) Compilation error on all lines
Answer: B
Explanation: Line 1 fails: private 'age' not accessible even in subclass. Line 2: protected
'weight' accessible in subclass. Line 3: public 'height' accessible everywhere.
Problem 5: Abstract Classes
• Difficulty: MEDIUM
• Concepts: Abstract classes, abstract methods, instantiation
Which code is VALID?
abstract class Shape { abstract void draw(); void display()
{ [Link]("Display"); } } class Circle extends Shape {
void draw() { [Link]("Drawing"); } }
A) Shape s = new Shape();
B) Circle c = new Circle(); [Link]();
C) Shape s = new Circle(); [Link]();
D) B and C are valid
Answer: D
Explanation: A fails: Cannot instantiate abstract class. B works: Circle is concrete. C works:
Superclass reference can point to subclass object.
Problem 6: Super Keyword & Variable Shadowing
• Difficulty: HARD
• Concepts: super() in constructors, variable shadowing
What is the output?
class Base { int x = 10; Base() { [Link]("Base: "
+ x); } } class Derived extends Base { int x = 20; Derived() {
super(); [Link]("Derived: " + x); } } public class
Test { public static void main(String[] args) { Derived d = new
Derived(); } }
A) Base: 10 / Derived: 20
B) Base: 20 / Derived: 20
C) Base: 10 / Derived: 10
D) Compilation Error
Answer: A
Explanation: Base constructor prints Base.x (10). Derived constructor prints Derived.x (20).
Each class has its own x variable.
Problem 7: Final Keyword Restrictions
• Difficulty: MEDIUM
• Concepts: Final classes, final methods, immutability
Which code will COMPILE ERROR?
• A) class Child extends FinalClass { }
• B) class Child extends RegularClass { void finalMethod() {} }
• C) TestClass t = new TestClass(); [Link] = 200;
• D) All of the above
Answer: D
Explanation: A: Cannot extend final class. B: Cannot override final method. C: Cannot modify
final variable. Final prevents all modifications.
Problem 8: Covariant Return Types
• Difficulty: HARD
• Concepts: Dynamic dispatch, covariant returns
What is the output?
class Animal { public Animal getMe()
{ [Link]("Animal"); return new Animal(); } }
class Dog extends Animal { public Dog getMe()
{ [Link]("Dog"); return new Dog(); } } public
class Test { Animal a = new Dog(); [Link](); }
A) Compilation Error
B) Animal
C) Dog
D) Runtime Exception
Answer: C
Explanation: Covariant return type: Dog's method returns Dog instead of Animal. Valid in Java
5+. Dynamic dispatch calls Dog's method.
Problem 9: Multi-level Inheritance
• Difficulty: HARD
• Concepts: Transitive inheritance, method hiding
What is the output?
class Level1 { void test() { [Link]("L1"); } } class Level2
extends Level1 { void test() { [Link]("L2"); } } class Level3
extends Level2 { } public class Test { Level1 l1 = new Level3();
[Link](); }
A) L1
B) L2
C) L3
D) Compilation Error
Answer: B
Explanation: Level3 inherits test() from Level2 (which overrides Level1). Dynamic dispatch
finds Level2's override in the inheritance chain.
Problem 10: Abstract Method Implementation
• Difficulty: HARD
• Concepts: Abstract methods, concrete subclasses
Which is INVALID?
• - Child1 extends Base and implements abstract method
• - Child2 is abstract and doesn't implement
• - Child3 extends Base and implements abstract method
A) Child1 only
B) Child2 only
C) Child3 only
D) All are valid
Answer: D
Explanation: All valid: Child1 (concrete), Child2 (abstract), Child3 (concrete) are all legal
patterns.
Key Concepts Summary
• Constructor Order: Superclass to subclass (always)
• Overriding vs Overloading: Same signature=override; different=overload
• Dynamic Dispatch: Actual object type determines which method
• Access: private<protected<public
• Super: super() calls parent constructor; [Link]() accesses parent
• Abstract: Cannot instantiate; must implement all abstract methods
• Final: Prevents inheritance, override, and modification