Inheritance is an OOPs concept in Java in which one class acquires the properties and methods of another class.
Inheritance is mainly used for:
- Code reusability
- Method overriding
- Achieving IS-A relationship
- Runtime polymorphism
The existing class is called:
Parent Class / Super Class / Base Class
The new class is called:
Child Class / Sub Class / Derived Class
class Child extends Parent
{
// child members
}Here Robo is the Parent/Super class.
It contains common methods:
move()
learn()
recharge()
interact()
FighterRobo, PlayerRobo, and TeacherRobo are child classes.
class Robo
{
public void move()
{
System.out.println("Robo moves fast");
}
public void learn()
{
System.out.println("Robo self learns");
}
public void recharge()
{
System.out.println("Plug in to recharge");
}
public void interact()
{
System.out.println("Robo interacts");
}
}
class FighterRobo extends Robo
{
public void fight()
{
System.out.println("Robo fights");
}
}
class PlayerRobo extends Robo
{
public void play()
{
System.out.println("Robo plays games");
}
}
class TeacherRobo extends Robo
{
public void teach()
{
System.out.println("Robo teaches");
}
} Robo
|
┌────────────┼────────────┐
↓ ↓ ↓
FighterRobo PlayerRobo TeacherRobo
| | |
fight() play() teach()
class RoboApp
{
public static void main(String[] args)
{
FighterRobo fr = new FighterRobo();
System.out.println("Fighter Robo");
fr.move();
fr.learn();
fr.recharge();
fr.interact();
fr.fight();
System.out.println("----------------");
PlayerRobo pr = new PlayerRobo();
System.out.println("Player Robo");
pr.move();
pr.learn();
pr.recharge();
pr.interact();
pr.play();
System.out.println("----------------");
TeacherRobo tr = new TeacherRobo();
System.out.println("Teacher Robo");
tr.move();
tr.learn();
tr.recharge();
tr.interact();
tr.teach();
}
}FighterRobo object can access:
move() → inherited
learn() → inherited
recharge() → inherited
interact() → inherited
fight() → own method
Similarly:
PlayerRobo
├── move() inherited
├── learn() inherited
├── recharge() inherited
├── interact() inherited
└── play() own method
TeacherRobo
├── move() inherited
├── learn() inherited
├── recharge() inherited
├── interact() inherited
└── teach() own method
One parent class → one child class.
Animal
↓
Dog
class Animal
{
public void eat()
{
System.out.println("Animal eats");
}
}
class Dog extends Animal
{
public void bark()
{
System.out.println("Dog barks");
}
}One parent class is extended by multiple child classes.
Animal
/ | \
↓ ↓ ↓
Dog Cat Cow
Your Robot program is an example of Hierarchical Inheritance:
Robo
/ | \
↓ ↓ ↓
FighterRobo PlayerRobo TeacherRobo
A class inherits from a parent class, and another class inherits from that child.
Exactly three levels:
Grand Parent
↓
Parent
↓
Child
Example:
class Animal
{
public void eat()
{
System.out.println("Animal eats");
}
}
class Dog extends Animal
{
public void bark()
{
System.out.println("Dog barks");
}
}
class Puppy extends Dog
{
public void play()
{
System.out.println("Puppy plays");
}
}Structure:
Animal
↓
Dog
↓
Puppy
Puppy can access accessible methods from both Dog and Animal.
Hybrid inheritance is a combination of two or more types of inheritance.
Example:
Animal
/ | \
↓ ↓ ↓
Herbivores Carnivores Omnivores
↓ ↓ ↓
Cow Tiger Dog
Here:
Animal
↓
Herbivores
↓
Cow
and:
Animal
↓
Carnivores
↓
Tiger
and:
Animal
↓
Omnivores
↓
Dog
The structure combines Hierarchical + Multilevel/Single relationships, so it is considered a hybrid structure.
The diamond structure is:
A
/ \
B C
\ /
D
Suppose A contains:
void show()and both B and C provide their own show().
Then D could receive two versions:
B → show()
C → show()
Now:
D.show()
creates ambiguity:
Which show() should be executed?
B's show() ?
OR
C's show() ?
This is called the Diamond Problem.
No.
Java does not allow one class to directly inherit from multiple classes, which avoids this classic ambiguity.
Java can achieve multiple inheritance of type using interfaces, subject to Java's conflict-resolution rules.
Cyclic inheritance means inheritance forms a circle.
Example:
A
↓
B
↓
C
↓
A
Or:
A → B
↑ ↓
└───┘
This is not permitted because there is no proper starting/root class.
Inheritance must form a hierarchy:
Parent
↓
Child
↓
Grandchild
not a cycle.
During inheritance, we commonly discuss:
Parent method is used by the child without changing its implementation.
class Robo
{
public void move()
{
System.out.println("Robo moves");
}
}
class FighterRobo extends Robo
{
}move() is an inherited method.
Parent has a method and child provides its own implementation with the same method signature.
class Robo
{
public void fight()
{
System.out.println("Robo fights");
}
}
class FighterRobo extends Robo
{
public void fight()
{
System.out.println("Fighter Robo fights strongly");
}
}fight() is overridden.
A method that exists only in the child class.
class Robo
{
public void move()
{
System.out.println("Robo moves");
}
}
class FighterRobo extends Robo
{
public void specialAttack()
{
System.out.println("Fighter Robo performs special attack");
}
}specialAttack() is a specialized method.
class Robo
{
public void move()
{
System.out.println("Robo moves");
}
public void fight()
{
System.out.println("Robo fights");
}
public void recharge()
{
System.out.println("Robo recharges");
}
}
class FighterRobo extends Robo
{
// Inherited: move()
// Inherited: recharge()
// Overridden method
public void fight()
{
System.out.println("Fighter Robo fights strongly");
}
// Specialized method
public void specialAttack()
{
System.out.println("Fighter Robo performs special attack");
}
}Robo
|
├── move() → Inherited
|
├── recharge() → Inherited
|
└── fight()
↓
Overridden
FighterRobo
|
└── specialAttack()
↓
Specialized
| Term | Meaning |
|---|---|
| Parent/Super class | Class whose members are inherited |
| Child/Sub class | Class that inherits from another class |
extends |
Keyword used to establish class inheritance |
| IS-A relationship | Relationship represented by inheritance |
| Inherited method | Parent method reused by child |
| Overridden method | Parent method reimplemented by child |
| Specialized method | New method introduced by child |
| Single | One parent → one child |
| Hierarchical | One parent → multiple children |
| Multilevel | Grandparent → Parent → Child |
| Hybrid | Combination of inheritance structures |
| Diamond problem | Ambiguity from multiple inheritance paths |
| Cyclic inheritance | Circular inheritance; not permitted |
INHERITANCE
|
Parent → Child
|
IS-A Relationship
|
┌──────────────┼──────────────┐
↓ ↓ ↓
Reuse Overriding Polymorphism
1. Single
A
↓
B
2. Hierarchical
A
/ | \
B C D
3. Multilevel
A
↓
B
↓
C
4. Hybrid
Combination of different inheritance structures
Robo
|
┌────────────┼────────────┐
↓ ↓ ↓
FighterRobo PlayerRobo TeacherRobo
| | |
fight() play() teach()
Inherited → Parent method used by child
Overridden → Child provides its own implementation
Specialized → Child creates a new method
Inheritance = Reusability + IS-A Relationship + Specialization + Polymorphism