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 MeowComparison: Without vs With inheritance
| Aspect | Without inheritance | With inheritance |
|---|---|---|
| Code duplication | Same fields/methods written in every class | Shared fields/methods live in one superclass |
| Maintenance | Change needed in many places | Change once in superclass, all subclasses benefit |
| Readability | Long, repetitive code | Clear 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‑arelationship (inheritance) andhas‑arelationship (composition).
📝 Likely Exam Questions
- Write a Java program to demonstrate inheritance using a
Vehiclesuperclass and aCarsubclass.
Model answer: Provide code similar to the Animal example, showingVehiclewith arun()method andCarextending it and addingdisplay(). - 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 ofsuperto access the parent version. - 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 isBbecause the object is of typeBand method overriding causes the subclass method to run. - List two advantages of using inheritance in Java.
Model answer: (1) Reduces code duplication, (2) Simplifies maintenance and enhances readability. - How do you call a superclass constructor from a subclass?
Model answer: Usesuper(parameters);as the first statement in the subclass constructor.