Why inheritance matters in Java
Imagine you could copy a whole set of traits from your parents without writing them again – that’s what inheritance does for Java classes, and it saves you tons of code.
Inheritance lets one class automatically acquire the fields (variables) and methods (functions) of another class. Think of a child inheriting eye colour and height from a parent – the child can use those traits without learning them from scratch.
What is inheritance?
Class is a blueprint for creating objects (real things you can work with in a program). An object is an actual instance of that blueprint. When we say a class inherits another, we mean it borrows the blueprint’s parts.
The class that gives away its parts is called the superclass (or parent class). The class that receives them is the subclass (or child class). In Java we use the extends keyword to create this relationship.
Simple syntax
```java class Animal { // Superclass void eat() { System.out.println("Eating"); } } class Dog extends Animal { // Subclass void bark() { System.out.println("Woof!"); } } ```
Here Dog automatically has the eat() method from Animal plus its own bark() method.
Why use inheritance?
- Re‑use code: No need to rewrite common behaviour.
- Logical grouping: Related classes sit together, making the program easier to read.
- Polymorphism (a fancy word for “many forms”): A single reference can point to objects of different subclasses, letting you write flexible code.
Key concepts you must know
1. Single inheritance
Java allows a class to extend only one superclass. This avoids the “diamond problem” where two parent classes could give conflicting instructions.
2. Method overriding
When a subclass defines a method with the same name and parameters as one in its superclass, the subclass’s version replaces the original when called on a subclass object. Use the @Override annotation to tell the compiler you meant to do this.
Example:
```java class Animal { void sound() { System.out.println("Some sound"); } } class Cat extends Animal { @Override void sound() { System.out.println("Meow"); } } ```
3. The super keyword
Inside a subclass you can call the superclass’s version of a method or constructor using super. It’s like saying, “Hey, parent, do your part first.”
```java class Parent { void greet() { System.out.println("Hello from Parent"); } } class Child extends Parent { void greet() { super.greet(); // Calls Parent's greet System.out.println("...and from Child"); } } ```
4. Access modifiers and inheritance
These words control who can see a class member:
- private: only the same class can use it – never inherited.
- protected: subclasses and classes in the same package can use it.
- public: everyone can use it.
Remember: a private field is still part of the object, but the subclass cannot access it directly.
Inheritance diagram – a quick visual
Think of a family tree drawn with boxes. The top box is the superclass, arrows point down to subclasses.
| Superclass | Subclass | What you get |
|---|---|---|
| Vehicle | Car | speed(), fuelLevel, startEngine() |
| Vehicle | Bike | speed(), fuelLevel, startEngine() |
| Car | Sedan | all Car features + trunkSpace |
When inheritance isn’t the best choice
Sometimes composition (having an object inside another) is cleaner. If two classes need the same behaviour but don’t share a logical “is‑a” relationship, prefer composition.
Quick bullet summary
- Use
extendsto create an inheritance relationship. - Superclass = parent, Subclass = child.
- Subclass inherits fields and methods (except private ones).
- Override methods to give subclass its own behaviour.
- Call
superto reuse the parent’s method or constructor. - Java supports only single inheritance to keep things simple.
📝 Likely Exam Questions
- Define inheritance in Java and give a simple code example.
Answer: Inheritance lets a subclass acquire fields and methods of a superclass using theextendskeyword. Example shown above withAnimalandDog. - What is method overriding and why is the @Override annotation useful?
Answer: Overriding is when a subclass provides its own version of a method defined in the superclass. @Override tells the compiler you intend to override, catching mismatched signatures. - Explain the role of the
superkeyword with an example.
Answer:superaccesses the superclass’s members. In theChildclass example,super.greet()runs the parent’s greet method before adding child‑specific output. - List three differences between private, protected, and public access modifiers in the context of inheritance.
Answer: Private members are not visible to subclasses; protected members are visible to subclasses and same‑package classes; public members are visible everywhere. - When would you prefer composition over inheritance?
Answer: When classes share behaviour but not an “is‑a” relationship, or when you need more flexibility and want to avoid the tight coupling that inheritance creates.