Why bother with inheritance?
Ever wondered how a child class can borrow its parent’s abilities without rewriting code? That’s the magic of inheritance in Java.
💡 In Simple Words: Inheritance lets one class (the child) automatically get the fields and methods of another class (the parent). Think of a smartphone inheriting all the features of a basic phone, then adding its own camera app.
What is inheritance in Java?
Inheritance is a way for a new class to acquire the properties (variables) and behaviors (methods) of an existing class. The existing class is called the parent (or superclass), and the new class is the child (or subclass).
Why does this matter? It saves you from typing the same code again and again, and it makes programs easier to maintain.
How inheritance works – syntax and keywords
To create a subclass, you write the extends keyword after the class name. The word extends simply means “is a kind of”.
class Parent {
int age;
void show(){ System.out.println("Parent"); }
}
class Child extends Parent {
void display(){ System.out.println("Child"); }
}
When Child extends Parent, it automatically gets age and show(). You can call them directly from a Child object.
Key points to remember
- Java supports single inheritance only – a class can extend just one parent.
- All classes ultimately inherit from
Object, the root of the Java class tree. - If a method in the child has the same name as one in the parent, the child’s version *overrides* the parent’s version.
Simple Java inheritance example – a real‑world analogy
Imagine a Vehicle class that has a method move(). A Car class extends Vehicle and adds a method playMusic(). The car can both move and play music, without having to rewrite the movement code.
class Vehicle {
void move(){ System.out.println("Moving..."); }
}
class Car extends Vehicle {
void playMusic(){ System.out.println("Playing music"); }
}
public class Test {
public static void main(String[] args){
Car myCar = new Car();
myCar.move(); // inherited from Vehicle
myCar.playMusic(); // defined in Car
}
}
Running this prints:
Moving...
Playing music
Benefits of using inheritance
- Reusability: Write once, use many times.
- Organization: Group common features in a parent, keep specific features in children.
- Maintainability: Fix a bug in the parent class and every child automatically gets the fix.
Inheritance vs Composition – quick compare
| Aspect | Inheritance | Composition |
|---|---|---|
| Relationship | "is‑a" (Car *is a* Vehicle) | "has‑a" (Car *has a* Engine) |
| Code reuse | Directly shares methods/fields | Uses objects of other classes |
| Flexibility | Fixed at compile time | Can change at runtime |
Common exam‑style questions
Below are the kinds of questions you might see in ICSE Class 10 Computer Applications, plus short model answers.
📝 Likely Exam Questions
- Write a class
Animalwith a methodsound(). Then create a subclassDogthat overridessound()to print "Bark".
class Animal { void sound(){ System.out.println("Some sound"); } } class Dog extends Animal { @Override void sound(){ System.out.println("Bark"); } } - Explain the difference between
extendsandimplementsin Java.
Extends is used for class inheritance (a class gets another class’s members). Implements is used for interfaces – a class promises to provide the methods declared in the interface.
- 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(); } }Output: B (method overriding – the actual object is of type B, so B’s version runs).
- List two advantages of using inheritance in Java.
- Code reusability – common code lives in one place.
- Easy maintenance – changes in the parent affect all children.
- Draw a simple class diagram showing a parent class
Personand two child classesStudentandTeacher.
A hand‑drawn diagram is enough; just showPersonat the top with an arrow labeled “extends” pointing to bothStudentandTeacher.
Practice these patterns, and the inheritance part of your Java exam will feel like a breeze.