Why Inheritance in Java Matters for Your ICSE Exams

Ever wished you could write one piece of code and have many classes reuse it? That’s exactly what inheritance lets you do, and it shows up in every Java question you’ll meet in the ICSE board exam.

💡 In Simple Words: Inheritance is like a family tree for classes. A child class automatically gets the traits (variables and methods) of its parent, so you don’t have to copy‑paste the same code over and over.

What Is Inheritance?

In Java, inheritance means a new class (called the subclass) takes on the properties of an existing class (called the superclass). The subclass can add its own features or change (override) what it inherited.

Key Terms Explained

  • Superclass – the class that gives away its features, like a parent.
  • Subclass – the class that receives those features, like a child.
  • Method overriding – when a subclass provides its own version of a method that already exists in the superclass.
  • Polymorphism – the ability to treat a subclass object as if it were an instance of its superclass.
  • Access modifier – a keyword (public, protected, private) that controls who can see a class member.
  • Constructor – a special method that runs when an object is created; it can be called from a subclass using super().
  • extends – the Java keyword that creates an inheritance relationship.

How the extends Keyword Works

Write the word extends between two class names and Java knows the second one is the child of the first. Only one class can be extended because Java follows a single‑inheritance rule, just like you can only have one set of biological parents.

class Parent { /* parent code */ }
class Child extends Parent { /* child code */ }

All public and protected members of Parent become part of Child automatically.

Java Inheritance Example – Animals and Dogs

Let’s see a real‑world example that you could copy into your notebook.

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

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

    // Overriding the eat method
    @Override
    void eat() {
        System.out.println(name + " is chewing a bone");
    }
}

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

When you run this, the output is:

Buddy is chewing a bone
Buddy says Woof!

Notice how Dog inherited the name variable and the original eat() method, then replaced eat() with its own version.

When to Use Inheritance

  • When several classes share common code – put that code in a superclass.
  • When you want a clear “is‑a” relationship (a Dog *is a* Animal).
  • When you need to implement polymorphic behavior in methods that accept a superclass type.

Things You Cannot Inherit

  • Constructors themselves – only the code inside them runs via super().
  • Private members – they stay hidden from subclasses.
  • Static methods that are not overridden – they belong to the class, not the object.

Quick Comparison Table

FeatureInheritedNot Inherited
Public methodsYes
Protected methodsYes
Private methodsNo
ConstructorsNo (but can call with super())
Static fieldsYes (but shared)

Inheritance Hierarchy Flowchart

graph TD A[Superclass] --> B[Subclass] B --> C[Sub‑Subclass] C --> D[Further Sub‑Class]

📝 Likely Exam Questions

  • Q1. Write a Java class Vehicle with a method move(). Then create a subclass Car that overrides move(). Show the code and the output when you call move() on a Car object.
  • Model Answer: (Provide code similar to the Animal example, with Vehicle and Car.) Output will be the overridden message from Car.
  • Q2. Explain the difference between a superclass and a subclass in one sentence each.
  • Model Answer: A superclass is the parent class that supplies fields and methods; a subclass is the child class that inherits those members and can add or modify them.
  • Q3. Which members are NOT inherited by a subclass? List two and give a short reason.
  • Model Answer: Private members are not inherited because they are hidden; constructors are not inherited because they are special methods used only to create objects.
  • Q4. What keyword do you use to create an inheritance relationship? Write a one‑line example.
  • Model Answer: Use extends. Example: class Dog extends Animal { }.
  • Q5. How does method overriding help achieve polymorphism? Answer in 2‑3 sentences.
  • Model Answer: Overriding lets a subclass provide its own version of a method declared in the superclass. When a superclass reference points to a subclass object, the overridden method runs, allowing the same call to behave differently for different objects.
#ICSE#Java#Inheritance#Class 10#Computer Applications