Why Java Inheritance matters for your ICSE exam

Ever wondered how a single piece of code can power many objects, just like a family trait passes from parents to kids? That’s the magic of inheritance in Java.

💡 In Simple Words: Inheritance lets one class (the child) automatically use fields and methods of another class (the parent). It’s like borrowing your older sibling’s video‑game controller – you get all the buttons without building a new one.

What is inheritance?

Inheritance is a feature that lets a new class acquire the properties (variables) and behaviours (methods) of an existing class. The existing class is called the superclass (or parent), and the new one is the subclass (or child).

Key terms, explained

  • Class: a blueprint that defines what an object can store (fields) and do (methods).
  • Object: an actual instance of a class, like a real car built from a car blueprint.
  • Superclass: the original class whose features are inherited.
  • Subclass: the new class that inherits and can add or change features.
  • Method overriding: when a subclass provides its own version of a method that already exists in the superclass.
  • super keyword: a special word used inside a subclass to refer to the superclass’s members.

How inheritance works in Java

Java uses the extends keyword to create a subclass. The syntax looks like this:

class Parent {
int age;
void display(){ System.out.println("Parent"); }
}

class Child extends Parent {
void display(){ System.out.println("Child"); } // overrides Parent's display()
}

When you create Child c = new Child();, the object c automatically has the age field from Parent and its own display() method.

Why use inheritance? – Real‑life analogy

Think of a school uniform policy. The school (superclass) decides the basic uniform: white shirt and navy trousers. Each grade (subclass) can add its own badge or tie, but they all still wear the core uniform. This saves the school from printing a completely new outfit for every grade.

Benefits for ICSE students

  • Code reuse: Write once, use many times – a big time‑saver for programming questions.
  • Logical organization: Group related classes together, making your code easier to read and debug.
  • Supports polymorphism: The same method call can behave differently for different subclasses – a common exam topic.

Simple Java inheritance example

Let’s model a basic animal hierarchy.

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

class Dog extends Animal {
void bark(){ System.out.println("Dog barks"); }
// inherits eat() from Animal
}

public class Test {
public static void main(String[] args){
Dog d = new Dog();
d.eat(); // prints Animal eats
d.bark(); // prints Dog barks
}
}

Notice that Dog didn’t need its own eat() method – it got it from Animal automatically.

Method overriding in action

If a subclass wants to change the behaviour of an inherited method, it simply defines a method with the same signature. The super keyword lets you still call the original version.

class Animal {
void sound(){ System.out.println("Some generic sound"); }
}

class Cat extends Animal {
void sound(){
super.sound(); // calls Animal's sound()
System.out.println("Meow");
}
}

Cat c = new Cat();
c.sound(); // Output: Some generic sound Meow

Comparison: Without vs With inheritance

AspectWithout inheritanceWith inheritance
Code duplicationSame fields/methods written in every classShared fields/methods live in one superclass
MaintenanceChange needed in many placesChange once in superclass, all subclasses benefit
ReadabilityLong, repetitive codeClear hierarchy, easier to follow

Quick checklist for ICSE Java inheritance questions

  • Identify the superclass (the class after extends).
  • Remember that private members are not inherited directly; they are hidden.
  • Check if method overriding is used – look for same method name and parameters.
  • Use super() to call the superclass constructor if needed.
  • Know the difference between is‑a relationship (inheritance) and has‑a relationship (composition).

📝 Likely Exam Questions

  1. Write a Java program to demonstrate inheritance using a Vehicle superclass and a Car subclass.
    Model answer: Provide code similar to the Animal example, showing Vehicle with a run() method and Car extending it and adding display().
  2. Explain method overriding with an example.
    Model answer: Define overriding, show a parent class method and a child class method with same signature, mention use of super to access the parent version.
  3. What is 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: Output is B because the object is of type B and method overriding causes the subclass method to run.
  4. List two advantages of using inheritance in Java.
    Model answer: (1) Reduces code duplication, (2) Simplifies maintenance and enhances readability.
  5. How do you call a superclass constructor from a subclass?
    Model answer: Use super(parameters); as the first statement in the subclass constructor.
#ICSE#Java#Inheritance#Class 10#Computer Applications