Bird
Raised Fist0
Javaprogramming~20 mins

Why inheritance is used in Java - Challenge Your Understanding

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
πŸŽ–οΈ
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do we use inheritance in Java?

Inheritance allows one class to take properties and methods from another class. Why is this useful?

ATo reuse code and create a relationship between classes
BTo make the program run faster by skipping methods
CTo hide all methods from other classes
DTo prevent any changes in the parent class
Attempts:
2 left
πŸ’‘ Hint

Think about how sharing common features can save time and effort.

❓ Predict Output
intermediate
2:00remaining
Output of inheritance example

What will be the output of this Java code?

Java
class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}
class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}
public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}
ARuntime error
BAnimal makes a sound
CCompilation error
DDog barks
Attempts:
2 left
πŸ’‘ Hint

Remember that the method called depends on the actual object type, not the reference type.

πŸ”§ Debug
advanced
2:00remaining
Identify the inheritance error

What error will this code produce?

Java
class Parent {
    void show() {
        System.out.println("Parent class");
    }
}
class Child extends Parent {
    void show(int x) {
        System.out.println("Child class: " + x);
    }
}
public class Test {
    public static void main(String[] args) {
        Child c = new Child();
        c.show();
    }
}
ARuntime error: NullPointerException
BOutput: Parent class
COutput: Child class: 0
DCompilation error: method show() not found in Child
Attempts:
2 left
πŸ’‘ Hint

Check if the Child class inherits the show() method without parameters.

πŸ“ Syntax
advanced
2:00remaining
Correct inheritance syntax

Which option shows the correct way to inherit a class in Java?

Aclass Child inherits Parent {}
Bclass Child implements Parent {}
Cclass Child extends Parent {}
Dclass Child uses Parent {}
Attempts:
2 left
πŸ’‘ Hint

Java uses a specific keyword to inherit from a class.

πŸš€ Application
expert
2:00remaining
Number of methods accessible via inheritance

Given these classes, how many methods can an object of class C call?

class A {
  void m1() {}
  void m2() {}
}
class B extends A {
  void m3() {}
}
class C extends B {
  void m4() {}
}
A4
B3
C2
D5
Attempts:
2 left
πŸ’‘ Hint

Count all methods from class C and its parents.

Practice

(1/5)
1. Why do we use inheritance in Java?
easy
A. To make programs run faster
B. To create unrelated classes
C. To reuse code from an existing class
D. To avoid using methods

Solution

  1. Step 1: Understand inheritance purpose

    Inheritance allows a new class to use code from an existing class, avoiding repetition.
  2. Step 2: Compare options

    Only To reuse code from an existing class correctly states code reuse. Others are incorrect because inheritance does not create unrelated classes, speed up programs directly, or avoid methods.
  3. Final Answer:

    To reuse code from an existing class -> Option C
  4. Quick Check:

    Inheritance = Code reuse [OK]
Hint: Inheritance means reusing code from another class [OK]
Common Mistakes:
  • Thinking inheritance speeds up the program
  • Believing inheritance creates unrelated classes
  • Confusing inheritance with method removal
2. Which of the following is the correct syntax to inherit class Animal in Java?
easy
A. class Dog inherits Animal {}
B. class Dog extends Animal {}
C. class Dog implements Animal {}
D. class Dog uses Animal {}

Solution

  1. Step 1: Recall Java inheritance syntax

    Java uses the keyword extends to inherit from a class.
  2. Step 2: Check each option

    class Dog extends Animal {} uses extends, which is correct. Options A, C, and D use wrong keywords.
  3. Final Answer:

    class Dog extends Animal {} -> Option B
  4. Quick Check:

    Inheritance keyword = extends [OK]
Hint: Use 'extends' keyword to inherit a class in Java [OK]
Common Mistakes:
  • Using 'inherits' instead of 'extends'
  • Confusing 'implements' with class inheritance
  • Using 'uses' keyword which doesn't exist
3. What will be the output of this Java code?
class Animal {
  void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
  void sound() { System.out.println("Bark"); }
}
public class Test {
  public static void main(String[] args) {
    Animal a = new Dog();
    a.sound();
  }
}
medium
A. Bark
B. Runtime error
C. Compilation error
D. Animal sound

Solution

  1. Step 1: Understand method overriding and polymorphism

    Dog overrides the sound() method of Animal. The object is of type Dog but referenced as Animal.
  2. Step 2: Determine which method runs

    At runtime, the Dog's sound() method runs due to polymorphism, printing "Bark".
  3. Final Answer:

    Bark -> Option A
  4. Quick Check:

    Overridden method runs = Bark [OK]
Hint: Overridden methods run from actual object type [OK]
Common Mistakes:
  • Expecting base class method output
  • Thinking it causes compile or runtime error
  • Confusing reference type with object type
4. Find the error in this inheritance code:
class Vehicle {
  void start() { System.out.println("Vehicle started"); }
}
class Car Vehicle {
  void start() { System.out.println("Car started"); }
}
medium
A. Class Vehicle should be abstract
B. Missing semicolon after class Vehicle
C. Method start() cannot be overridden
D. Incorrect inheritance syntax in class Car

Solution

  1. Step 1: Check class inheritance syntax

    In Java, to inherit a class, use extends keyword. The code misses extends in class Car Vehicle.
  2. Step 2: Verify other options

    No semicolon needed after class declaration, methods can be overridden, and Vehicle need not be abstract.
  3. Final Answer:

    Incorrect inheritance syntax in class Car -> Option D
  4. Quick Check:

    Use 'extends' keyword for inheritance [OK]
Hint: Inheritance needs 'extends' keyword in class declaration [OK]
Common Mistakes:
  • Forgetting 'extends' keyword
  • Adding semicolon after class header
  • Thinking methods can't be overridden
5. You want to create a class SmartPhone that has all features of Phone plus new features like camera and GPS. Which is the best way to do this using inheritance?
hard
A. Make SmartPhone extend Phone and add new methods
B. Make Phone extend SmartPhone and add new methods
C. Create SmartPhone and Phone as separate classes with no relation
D. Copy all Phone code into SmartPhone without inheritance

Solution

  1. Step 1: Identify real-world relationship

    A SmartPhone is a type of Phone with extra features, so it should inherit Phone.
  2. Step 2: Apply inheritance correctly

    SmartPhone should extend Phone and add new methods for camera and GPS.
  3. Final Answer:

    Make SmartPhone extend Phone and add new methods -> Option A
  4. Quick Check:

    Subclass adds features to superclass [OK]
Hint: Subclass extends superclass to add features [OK]
Common Mistakes:
  • Reversing inheritance direction
  • Not using inheritance for related classes
  • Copy-pasting code instead of extending