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
| Term | Meaning |
|---|---|
| Superclass | The class whose members are inherited. |
| Subclass | The class that inherits and can add/modify members. |
| extends | Keyword that creates an inheritance relationship. |
| Method overriding | Providing a new version of a method that already exists in the superclass. |
| Polymorphism | Ability to treat a subclass object as if it were an instance of its superclass. |
How Inheritance Works – Simple Flowchart
Things to Remember for ICSE Exams
- Every subclass automatically gets
publicandprotectedmembers of its superclass. - Constructors are not inherited; you must call
super()explicitly if needed. - Use
@Overrideannotation to avoid accidental mistakes when overriding. - Only one class can be extended (no multiple inheritance).
📝 Likely Exam Questions
- Write a class
Vehiclewith a methodmove(). Then create a subclassCarthat adds a methodhonk(). Show how to call both methods from aCarobject.
Model answer: Provide code similar to the Animal/Dog example, replacing names accordingly. - 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. - 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 printsBbecause the overridden method inBis called at runtime (dynamic polymorphism). - 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 usingsuper(). - State the purpose of the
extendskeyword in a single sentence.
Model answer:extendscreates an inheritance link so that the child class automatically receives the parent’s members.