Inheritance in Java: Complete Guide with Types, Examples, and Best Practices
Introduction
One of the chief Object-Oriented Programming (OOP) concepts that a developer must learn is inheritance in Java. Inheritance allows one class to inherit attributes and methods (fields and methods) of another class. This can make your code reusable, more organized, and easier to maintain.
Table Of Content
- Introduction
- What is Inheritance in Java?
- Syntax of Inheritance in Java
- Types of Inheritance in Java
- Single Inheritance in Java
- Multilevel Inheritance in Java
- Hierarchical Inheritance in Java
- Hybrid Inheritance in Java (via Interfaces)
- Multiple Inheritance in Java (via Interfaces)
- Method Overriding and Super Keyword
- Real-Life Example of Inheritance in Java
- Advantages of Inheritance in Java
- Common Mistakes and Best Practices
- FAQs
- Conclusion
- Related Links
In this article, we will explain inheritance in Java, describe types of inheritance in Java, and provide examples and tips.
What is Inheritance in Java?
Inheritance is a construct where one class (the subclass or child class) can inherit attributes and methods from another class (the superclass or parent class).

Inheritance can also:
- Promote code reuse – write once, use any time.
- Enable extensibility – can easily add or override functionality.
- Allow polymorphism – can utilize dynamic method dispatch (this means we can make a single method called that can be overridden at runtime).
The cool thing about inheritance in Java is that it uses the keyword extends.
Syntax of Inheritance in Java
class Parent {
void display() {
System.out.println("This is the parent class");
}
}
class Child extends Parent {
void show() {
System.out.println("This is the child class");
}
}
public class TestInheritance {
public static void main(String[] args) {
Child obj = new Child();
obj.display(); // Inherited from Parent
obj.show(); // Childβs own method
}
}
Types of Inheritance in Java

Java supports several forms of inheritance. However, multiple inheritance with classes is not allowed (to avoid ambiguity), but you can achieve it using interfaces.
Single Inheritance in Java
A single child class inherits from a single parent class.

class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
Multilevel Inheritance in Java

A class inherits from another child class, forming a chain.
class Animal { void eat() {} }
class Dog extends Animal { void bark() {} }
class Puppy extends Dog { void weep() {} }
Here, Puppy inherits from Dog, and Dog inherits from Animal.
Hierarchical Inheritance in Java

Multiple child classes inherit from a single parent.
class Animal { void eat() {} }
class Dog extends Animal { void bark() {} }
class Cat extends Animal { void meow() {} }
Hybrid Inheritance in Java (via Interfaces)

While Java doesnβt allow true hybrid inheritance with classes, you can achieve it using interfaces.
interface A { void methodA(); }
interface B { void methodB(); }
class C implements A, B {
public void methodA() {}
public void methodB() {}
}
π Hybrid inheritance is useful when you need functionality from multiple sources.
Multiple Inheritance in Java (via Interfaces)

Java does not support multiple inheritance with classes because it leads to ambiguity (commonly known as the diamond problem).
However, you can achieve multiple inheritance using interfaces.
interface Printer {
void print();
}
interface Scanner {
void scan();
}
class MultiFunctionMachine implements Printer, Scanner {
public void print() {
System.out.println("Printing document...");
}
public void scan() {
System.out.println("Scanning document...");
}
}
public class TestMultipleInheritance {
public static void main(String[] args) {
MultiFunctionMachine m = new MultiFunctionMachine();
m.print();
m.scan();
}
}
β Key point: Multiple inheritance in Java is supported only through interfaces, not through regular classes.
Method Overriding and Super Keyword
When a subclass provides a specific implementation of a method already defined in its parent, itβs called method overriding.
class Parent {
void message() {
System.out.println("Message from Parent");
}
}
class Child extends Parent {
@Override
void message() {
System.out.println("Overridden message from Child");
}
}
Use the super keyword to access the parent classβs members.
Real-Life Example of Inheritance in Java

Consider vehicles:
- A Vehicle class may define attributes like speed and fuel.
- A Car class may inherit from Vehicle and add air conditioning.
- A Sports Car class may inherit from Car and add turbo boost.
This hierarchy demonstrates how inheritance in Java represents real-world relationships.
Advantages of Inheritance in Java
- Reduces duplication of code.
- Makes programs easier to maintain and extend (scalable).
- Provides the basis for polymorphism, which allows run-time flexibility
Common Mistakes and Best Practices
- Avoid long inheritance hierarchies (difficult to debug).
- Use composition over inheritance whenever the “is-a” relationship is unclear.
- Be sure to use the @Override annotation to catch errors.
- Keep parent classes generic.
FAQs
Q1: Can we inherit constructors in Java?
No, constructors are not inherited, but they can be called from the subclass by using super().
Q2: Does Java support multiple inheritance?
Not with classes, but it does with interfaces.
Q3: How do inheritance and polymorphism connect?
Polymorphism depends on inheritance to override methods.
Conclusion
As you can see, mastering inheritance in Java is necessary for writing clean, reusable, and extensible code. Understanding the types of inheritance in Java, method overriding, and best practices is half the battle. With the right practices, you will end up creating great applications that conform to solid OOPs in Java.
Continue to practice using a hierarchy like what we see in real-life, but remember to use both inheritance and composition!
If you are looking to improve your skills, consider enrolling in a complete Java course that explores inheritance, polymorphism, and all major OOPs in Java concepts with guided hands-on projects.

