Ever wondered how a child can automatically inherit a parent’s traits? Java does the same with classes!
💡 In Simple Words: Inheritance lets one class (the child) automatically get the variables and methods of another class (the parent). Think of it as a kid getting their eye colour and height from Mom and Dad without writing those details again.
What is Inheritance in Java?
Inheritance is a way for a new class to reuse code from an existing class. The existing class is called the parent or superclass, and the new one is the child or subclass. The child can add its own features while keeping everything it inherited.
How does Java implement inheritance?
Java uses the extends keyword. When you write class Dog extends Animal, you tell Java that Dog should inherit everything from Animal.
Basic syntax
class ParentClass {
int age;
void display(){
System.out.println("Age: "+age);
}
}
class ChildClass extends ParentClass {
String name;
void show(){
System.out.println("Name: "+name);
}
}
Here ChildClass automatically has the age variable and the display() method, even though we never wrote them again.
Types of Inheritance in Java
Java officially supports three main kinds. (Hybrid combines them, but it’s just a mix.)
| Type | How it looks | Example |
|---|---|---|
| Single | One child inherits from one parent. | Dog extends Animal |
| Multilevel | Chain of inheritance: Grandchild → Child → Parent. | Bulldog extends Dog extends Animal |
| Hierarchical | Many children share the same parent. | Dog extends Animal, Cat extends Animal |
Key keywords you’ll see
- extends: tells Java which class to inherit from.
- super: a reference to the parent’s members. Use
super()to call the parent’s constructor, orsuper.method()to call a hidden method. - overriding: when the child defines a method with the same signature as the parent, the child’s version runs instead.
Using super – a quick example
class Animal {
String sound = "generic sound";
void makeSound(){
System.out.println(sound);
}
}
class Dog extends Animal {
String sound = "Bark"; // hides parent variable
void makeSound(){
super.makeSound(); // prints "generic sound"
System.out.println(sound); // prints "Bark"
}
}
Calling new Dog().makeSound(); prints:
generic sound Bark
The super.makeSound() part lets us use the parent’s version before adding our own twist.
Why use inheritance?
- Code reusability: Write once, use many times.
- Logical hierarchy: Mirrors real‑world relationships, making programs easier to understand.
- Maintainability: Fix a bug in the parent class and every child gets the fix automatically.
Common pitfalls
- Java does not support multiple inheritance with classes (a class can’t extend two parents). It solves this with interfaces.
- Be careful with method overriding – if you forget
@Overrideannotation, you might think you’re overriding when you’re actually overloading. - Never make a parent class too “fat”. Keep it focused; otherwise every child inherits unnecessary stuff.
Quick recap – bullet summary
- Inheritance = child gets everything from parent.
- Use
extendsto create a subclass. - Three main types: single, multilevel, hierarchical.
superlets you reach the parent’s members.- Benefits: reuse, clearer structure, easier maintenance.
📝 Likely Exam Questions
- Define inheritance in Java and give a real‑world analogy.
Answer: Inheritance is a mechanism where a subclass automatically acquires the fields and methods of a superclass. It’s like a child inheriting eye colour and height from parents. - Write a Java program where class
Caris the parent of classElectricCar. Show how to call the parent constructor usingsuper.
Answer:class Car { int speed; Car(int s){ speed = s; } void display(){ System.out.println("Speed: "+speed); } } class ElectricCar extends Car { int battery; ElectricCar(int s, int b){ super(s); // calls Car constructor battery = b; } void show(){ display(); System.out.println("Battery: "+battery); } } - What is method overriding? Write a short example.
Answer: Overriding is when a subclass provides its own implementation of a method that already exists in the superclass. Example:class Animal{void sound(){System.out.println("Generic");}} class Dog extends Animal{void sound(){System.out.println("Bark");}} - List three advantages of using inheritance.
Answer: (i) Code reusability, (ii) Logical hierarchy that mirrors real‑world relationships, (iii) Easier maintenance because changes in the superclass affect all subclasses. - Explain why Java does not allow a class to extend more than one class.
Answer: Multiple inheritance can cause ambiguity (e.g., which parent’s method should be used). Java avoids this complexity by allowing a class to extend only one class and using interfaces for multiple type inheritance.