What is Inheritance in Java?

Inheritance is a way for one class (a blueprint for objects) to borrow properties and behaviours from another class. The class that gives away its code is called the superclass (or parent), and the class that receives it is the subclass (or child).

Think of a family tree: just as children inherit eye colour or height from their parents, a subclass automatically gets the methods and variables of its superclass.

đź’ˇ In Simple Words: In Java, inheritance lets a new class reuse code from an existing class. The new class is a child, the old one is a parent, and the child automatically knows the parent's abilities.

Why Use Inheritance?

  • Code reuse: Write common code once in a parent class and let many children use it.
  • Easy maintenance: Change a method in the parent and every child sees the change.
  • Logical grouping: Group similar objects (like all vehicles) under one umbrella.

Syntax of Inheritance in Java

The keyword that creates the parent‑child link is extends. The basic form looks like this:

class ChildClass extends ParentClass {
    // child‑specific code
}

Only one class can be extended at a time (single inheritance). If you need more, you use interfaces, but that’s a different story.

Simple Java Inheritance Example

Let’s see a real snippet that you could type into the IDE.

class Animal {
    void eat() {
        System.out.println("Animal is eating");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog says: Woof!");
    }

    // Overriding the eat method
    @Override
    void eat() {
        System.out.println("Dog is eating kibble");
    }
}

public class TestInheritance {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.eat();   // Calls Dog's overridden method
        d.bark();  // Calls Dog's own method
    }
}

When Dog is created, it automatically has the eat() method from Animal. We then replace (override) that method to give a more specific behaviour for dogs.

Key Terms at a Glance

TermMeaning
SuperclassThe class whose members are inherited.
SubclassThe class that inherits and can add/modify members.
extendsKeyword that creates an inheritance relationship.
Method overridingProviding a new version of a method that already exists in the superclass.
PolymorphismAbility to treat a subclass object as if it were an instance of its superclass.

How Inheritance Works – Simple Flowchart

graph TD\nA[Superclass] --> B[Subclass] --> C[Sub‑subclass]

Things to Remember for ICSE Exams

  • Every subclass automatically gets public and protected members of its superclass.
  • Constructors are not inherited; you must call super() explicitly if needed.
  • Use @Override annotation to avoid accidental mistakes when overriding.
  • Only one class can be extended (no multiple inheritance).

📝 Likely Exam Questions

  1. Write a class Vehicle with a method move(). Then create a subclass Car that adds a method honk(). Show how to call both methods from a Car object.
    Model answer: Provide code similar to the Animal/Dog example, replacing names accordingly.
  2. Explain the difference between method overriding and method overloading.
    Model answer: Overriding changes a method’s behaviour in a subclass with the same signature; overloading creates multiple methods with the same name but different parameters in the same class.
  3. What will be the output of the following code?
    class A { void show(){System.out.println("A");}} class B extends A { void show(){System.out.println("B");}} public class Test{public static void main(String[] args){A obj = new B(); obj.show();}}

    Model answer: It prints B because the overridden method in B is called at runtime (dynamic polymorphism).
  4. Why can a subclass not inherit the constructor of its superclass?
    Model answer: Constructors are meant to initialise the specific class’s fields; inheriting them would confuse which class’s state is being set. Instead, a subclass calls the superclass constructor using super().
  5. State the purpose of the extends keyword in a single sentence.
    Model answer: extends creates an inheritance link so that the child class automatically receives the parent’s members.
#ICSE#Class 10#Java#Inheritance#Computer Applications