Inheritance helps us reuse code and organize related things easily. It lets one class get features from another without rewriting them.
Why inheritance is used in Java
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Java
class ParentClass { // common features } class ChildClass extends ParentClass { // new or specialized features }
The extends keyword shows that one class inherits from another.
The child class gets all the features of the parent class automatically.
Examples
Java
class Animal { void eat() { System.out.println("Eating food"); } } class Dog extends Animal { void bark() { System.out.println("Barking"); } }
Java
class Vehicle { void move() { System.out.println("Moving"); } } class Car extends Vehicle { void honk() { System.out.println("Honking"); } }
Sample Program
This program shows a Dog object using both its own method and the inherited method from Animal.
Java
class Animal { void eat() { System.out.println("Eating food"); } } class Dog extends Animal { void bark() { System.out.println("Barking"); } } public class Main { public static void main(String[] args) { Dog myDog = new Dog(); myDog.eat(); // inherited method myDog.bark(); // own method } }
Important Notes
Inheritance helps avoid repeating code in many classes.
Use inheritance only when there is a clear 'is-a' relationship.
Java supports single inheritance for classes, meaning one class can extend only one parent class.
Summary
Inheritance lets a class get features from another class.
It helps reuse code and organize related classes.
Use inheritance to model real-world relationships and add new features easily.
Practice
1. Why do we use inheritance in Java?
easy
Solution
Step 1: Understand inheritance purpose
Inheritance allows a new class to use code from an existing class, avoiding repetition.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.Final Answer:
To reuse code from an existing class -> Option CQuick 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
Solution
Step 1: Recall Java inheritance syntax
Java uses the keywordextendsto inherit from a class.Step 2: Check each option
class Dog extends Animal {} usesextends, which is correct. Options A, C, and D use wrong keywords.Final Answer:
class Dog extends Animal {} -> Option BQuick 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
Solution
Step 1: Understand method overriding and polymorphism
Dog overrides thesound()method of Animal. The object is of type Dog but referenced as Animal.Step 2: Determine which method runs
At runtime, the Dog'ssound()method runs due to polymorphism, printing "Bark".Final Answer:
Bark -> Option AQuick 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
Solution
Step 1: Check class inheritance syntax
In Java, to inherit a class, useextendskeyword. The code missesextendsinclass Car Vehicle.Step 2: Verify other options
No semicolon needed after class declaration, methods can be overridden, and Vehicle need not be abstract.Final Answer:
Incorrect inheritance syntax in class Car -> Option DQuick 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
Solution
Step 1: Identify real-world relationship
A SmartPhone is a type of Phone with extra features, so it should inherit Phone.Step 2: Apply inheritance correctly
SmartPhone should extend Phone and add new methods for camera and GPS.Final Answer:
Make SmartPhone extend Phone and add new methods -> Option AQuick 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
