LearnFrenzy
Sign in Take a free test

OOPs · Topic 3 of 10

Inheritance: questions with answers

is-a relationships, constructor and destructor order, super calls, types of inheritance, field and static hiding, multiple inheritance, the diamond problem, MRO, slicing and the fragile base class.

Questions
45
Timed sets
3
Answers run or computed
36
Your accuracy
–

What this topic covers

17 subtopics
  • Multiple inheritance9
  • Constructor order5
  • super4
  • Access in subclasses3
  • Basics3
  • Hiding3
  • Method resolution3
  • Restricting inheritance3
  • Design2
  • Overriding rules2
  • Type checks2
  • Destructors1
Show all 17 subtopics
  • Fragile base class1
  • Liskov substitution1
  • Mixins1
  • Slicing1
  • Types of inheritance1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

Inheritance lets a class reuse and specialise another: a Car is a Vehicle. Output puzzles test the order in which constructors and destructors run, what super calls, and which members are actually polymorphic – methods are, but fields and static methods are only hidden. Multiple inheritance brings the diamond problem, solved differently by C++ (virtual bases), Java (interfaces with default methods) and Python (the method resolution order). Hierarchies are drawn as UML class diagrams.

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 Constructor order Beginner Practice Code executed

What does this print?

Class diagram
VehicleCarSportsCar
Java Main.java
public class Main {
    static class Vehicle { Vehicle() { System.out.print("Vehicle "); } }
    static class Car extends Vehicle { Car() { System.out.print("Car "); } }
    static class SportsCar extends Car { SportsCar() { System.out.print("SportsCar "); } }

    public static void main(String[] args) {
        new SportsCar();
    }
}
Vehicle SportsCar
SportsCar Car Vehicle
SportsCar
Vehicle Car SportsCar
Show answer and explanation
Correct answer D

Explanation

Every constructor first calls its parent's constructor (an implicit super()), so construction runs from the top of the hierarchy down.

Why the other options are wrong

  • ACar's constructor is not skipped.
  • BParents are built before children.
  • CThe parent constructors run too.
Question 2 Hiding Intermediate Practice Code executed

Both classes declare a field name. What does this print?

Java Main.java
public class Main {
    static class Parent {
        String name = "parent";
        String getName() { return name; }
    }
    static class Child extends Parent {
        String name = "child";
        @Override String getName() { return name; }
    }

    public static void main(String[] args) {
        Parent p = new Child();
        System.out.println(p.name + " " + p.getName() + " " + ((Child) p).name);
    }
}
parent parent child
child parent parent
child child child
parent child child
Show answer and explanation
Correct answer D

Explanation

Fields are not polymorphic: p.name is chosen by the reference type (Parent). Methods are dispatched by the object's real type. The Child object actually holds two separate name fields.

Why the other options are wrong

  • AgetName is overridden.
  • BThe cast selects Child's field.
  • CField access uses the static type.
Question 3 Multiple inheritance Advanced Practice Code executed

Two interfaces provide the same default method. What does this print?

Java Main.java
public class Main {
    interface Walker  { default String move() { return "walk"; } }
    interface Swimmer { default String move() { return "swim"; } }

    static class Duck implements Walker, Swimmer {
        @Override public String move() {
            return Walker.super.move() + "+" + Swimmer.super.move();
        }
    }

    public static void main(String[] args) {
        Walker w = new Duck();
        System.out.println(w.move());
    }
}
Compile error: ambiguous move()
walk+swim
walk
swim
Show answer and explanation
Correct answer B

Explanation

When two interfaces supply conflicting defaults, the class must override the method (otherwise it does not compile) and can call either one with Interface.super.method(). That is Java's answer to the diamond problem.

Why the other options are wrong

  • AThe explicit override resolves the conflict.
  • CThe override combines both.
  • DThe override combines both.
42 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

OOPs

All 10 topics

Topics, levels and full mocks
  1. 01 Classes, Objects and Constructors45 questions · 3 levels
  2. 02 Encapsulation and Access Modifiers45 questions · 3 levels
  3. 03 Inheritance45 questions · 3 levels
  4. 04 Polymorphism45 questions · 3 levels
  5. 05 Abstract Classes and Interfaces45 questions · 3 levels
  6. 06 Object Lifecycle and Copying45 questions · 3 levels
  7. 07 static, final, this and super45 questions · 3 levels
  8. 08 Exception Handling45 questions · 3 levels
  9. 09 SOLID and Class Relationships45 questions · 3 levels
  10. 10 Design Patterns45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links