Understanding Inheritance in Java
with Real-Life Examples
What is Inheritance
Inheritance is a concept of Object-Oriented Programming (OOP) because it
allows the creation of hierarchical classification. Using inheritance, you can create
a general class that defines traits common to a set of related items. This class can
then be inherited by other, more specific classes, each adding features that are
unique to it.
In the terminology of Java, a class that is inherited is called a superclass (or base
class). The class that does the inheriting is called a subclass(or derived class).
Therefore, a subclass is a specialized version of a superclass. It inherits all public
and protected members defined by the superclass and adds its own unique elements.
ADVANTAGES OF
INHERITANCE
•Promotes code reusability
•Simplifies code maintenance
•Supports method overriding for better
customization
DISADVANTAGES OF INHERITANCE
Increased Complexity
Overriding Can Lead to Unexpected Behavior
Types of Inheritance in Java
o Java does not support multiple/hybrid
inheritance using classes, but it can be
achieved using interfaces
o This restriction exists because, for example, if
two parent classes have a function with the
same name and signature, the compiler
would be confused about which version of
the function to execute. This leads to
ambiguity, which is known as the diamond
problem in multiple inheritance.
SINGLE INHERITANCE
In single inheritance, a
sub-class is derived
from only one super
class. It inherits the
properties and behavior
of a single-parent class
MULTILEVEL INHERITANCE
o Multilevel inheritance is when a class inherits
from another derived class.
o In simple words, the Grandchild class is created
based on the Child class, which already inherits
from the Parent class.
o This forms a chain-like structure where the
Grandchild class indirectly gets the features of the
Parent class through the Child class.
HIRERCHICAL INHERITANCE
In the image:
Class A is the base (parent) class.
Class B and Class C both inherit from Class A.
Then, Class D and Class E inherit from Class B,
and Class F and Class G inherit from Class C.
So, one parent class (Class A) gives its
properties to many child classes, forming a tree-
like structure.
OVERRIDING
Method overriding in Java allows a subclass to provide a specific
implementation of a method that is already defined in its
superclass. The overriding method in the subclass must have
the same name, parameters, and return type as the method in
the parent class. This enables the subclass to modify or extend
the behavior of the inherited method,
Why use @Override in Java?
o Tells Java you're replacing a parent class
method
o Helps avoid mistakes (Java checks
method signature)
o Improves readability and makes
maintenance easier
o Without it, Java won’t warn you if you
mess up the method name
How to access a parent class method in Java via
grandchild?
SUMMARY
Inheritance allows a child class to reuse features of a
parent class, which helps reduce code duplication and
improve code organization. However, if inheritance is
used where it doesn't fit logically, it can lead to confusing
code, tight coupling, and unexpected errors.
That's why inheritance should only be used when the
child class truly represents a more specific version of the
parent class — for example:
✅ ‘Car is a Vehicle’ or ‘BossEnemy is an Enemy’
❌ ‘Car is an Engine’ or ‘Bullet is a HealthPack’