What is Inheritance in Java?

Think of a child inheriting traits from its parents. In Java, a class can inherit fields (variables) and methods (functions) from another class. This saves you from writing the same code over and over.

đź’ˇ In Simple Words: Inheritance lets one class borrow code from another class, just like a kid gets eye colour and height from mom and dad. The new class (child) can add its own features while keeping the old ones.

Why do we need Inheritance?

It makes programs easier to read, changes are quicker, and you avoid duplication. Imagine you have many types of vehicles. If each type had its own speed() method written from scratch, a tiny mistake could creep in. With inheritance, you write speed() once in a generic Vehicle class and let Car, Bike, Truck reuse it.

Basic Syntax

To inherit, you use the extends keyword. extends means “goes beyond”. The parent class is called the superclass (the class you’re extending), and the child class is the subclass (the class that extends).

class Vehicle {               // Superclass
    int speed;
    void setSpeed(int s){ speed = s; }
}
class Car extends Vehicle {   // Subclass
    int seats;
    void setSeats(int n){ seats = n; }
}

Now Car has both speed and seats. You can call setSpeed() on a Car object even though the method lives in Vehicle.

Types of Inheritance in Java

TypeHow it works
SingleOne subclass inherits from one superclass.
MultilevelChain of inheritance – a subclass becomes a superclass for another subclass.
HierarchicalMany subclasses inherit from the same superclass.
HybridMix of multilevel and hierarchical (Java doesn’t allow multiple inheritance of classes, but interfaces can be combined).

Real‑world example: Vehicles

Let’s see a small family tree of classes.

graph TD Vehicle[Vehicle] --> Car[Car] Vehicle --> Bike[Bike] Car --> ElectricCar[Electric Car] Bike --> MountainBike[Mountain Bike]

Every class below automatically gets the speed field and setSpeed() method from Vehicle. ElectricCar can add a battery‑capacity field, while MountainBike can add a suspension type.

Using super and this

Sometimes the child class wants to use the parent’s version of a method or constructor. The keyword super points to the parent, while this points to the current object.

class Car extends Vehicle {
    Car(int s){               // Constructor
        super.setSpeed(s);   // Call parent’s method
    }
    void display(){
        System.out.println("Speed: "+speed); // speed comes from Vehicle
    }
}

If you forget super and write speed = s; directly, it still works because speed is inherited, but using super makes your intent clear.

Common pitfalls

  • Trying to inherit from a final class – final means “cannot be extended”.
  • Overriding a private method – private members are invisible to subclasses.
  • Confusing method overloading (same name, different parameters) with overriding (same signature).

Quick Revision Checklist

  • Keyword for inheritance: extends
  • Parent class = superclass, child class = subclass
  • Single, multilevel, hierarchical, hybrid types
  • Use super to call parent constructors or methods
  • Java doesn’t support multiple inheritance of classes (use interfaces instead)

📝 Likely Exam Questions

  1. What is inheritance in Java? Give a short definition.
    Answer: Inheritance is a mechanism where a subclass acquires fields and methods of a superclass, allowing code reuse.
  2. Write the syntax to create a subclass named Bike that inherits from Vehicle.
    Answer: class Bike extends Vehicle { }
  3. Explain the difference between single and multilevel inheritance with an example.
    Answer: Single inheritance has one parent‑child link (e.g., Car extends Vehicle). Multilevel inheritance forms a chain (e.g., ElectricCar extends Car extends Vehicle).
  4. Why can’t Java support multiple inheritance of classes? Mention one way to achieve similar behaviour.
    Answer: Multiple inheritance can cause ambiguity when two parent classes have the same method. Java uses interfaces, which a class can implement multiple of, to achieve similar functionality.
  5. What does the super keyword do? Provide a short code snippet.
    Answer: super refers to the immediate superclass. Example: super.setSpeed(s); inside a subclass constructor calls the parent’s method.
#ICSE#Java#Inheritance#Class 10#Computer Applications