Java Inheritance Types – Complete Guide with Examples
Learn all types of inheritance in Java, including single, multilevel, hierarchical, and hybrid inheritance, with detailed examples and best practices in object-oriented programming.
Inheritance Types in Java – Complete Detailed Tutorial
Inheritance is a key feature of Object-Oriented Programming (OOP).
It allows a class (child/subclass) to inherit fields and methods from another class (parent/superclass).
This promotes code reuse and modularity.
1. Advantages of Inheritance
- Code Reusability: Use existing methods and fields in new classes
- Method Overriding: Customize inherited methods
- Extensibility: Easy to extend programs
- Data Hiding: Parent class can restrict access using access modifiers
2. Syntax of Inheritance
class Parent {
// fields and methods
}
class Child extends Parent {
// inherits fields and methods from Parent
}
extendskeyword is used to inherit a class- Java supports single, multilevel, hierarchical inheritance
- Multiple inheritance with classes is not supported (avoids ambiguity)
- Interfaces can provide multiple inheritance
3. Types of Inheritance in Java
3.1 Single Inheritance
- A child class inherits from a single parent class
Example – Single Inheritance:
class Animal {
void eat() {
System.out.println("Animal eats food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // inherited from Animal
d.bark(); // own method
}
}
Output:
Animal eats food
Dog barks
3.2 Multilevel Inheritance
- A class inherits from a child class, forming a chain
Example – Multilevel Inheritance:
class Animal {
void eat() {
System.out.println("Animal eats food");
}
}
class Mammal extends Animal {
void walk() {
System.out.println("Mammal walks");
}
}
class Dog extends Mammal {
void bark() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // from Animal
d.walk(); // from Mammal
d.bark(); // from Dog
}
}
Output:
Animal eats food
Mammal walks
Dog barks
3.3 Hierarchical Inheritance
- Multiple child classes inherit from a single parent class
Example – Hierarchical Inheritance:
class Animal {
void eat() {
System.out.println("Animal eats food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
Cat c = new Cat();
c.eat();
c.meow();
}
}
Output:
Animal eats food
Dog barks
Animal eats food
Cat meows
3.4 Multiple Inheritance (Through Interfaces)
- Java does not support multiple inheritance with classes to avoid ambiguity
- Interfaces allow multiple inheritance
Example – Multiple Inheritance via Interface:
interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
public void methodA() {
System.out.println("Method A");
}
public void methodB() {
System.out.println("Method B");
}
}
public class Main {
public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
}
}
Output:
Method A
Method B
3.5 Hybrid Inheritance (Combination)
- Combination of multiple inheritance types
- Achieved using interfaces to avoid class-based multiple inheritance
Example:
interface X {
void showX();
}
interface Y {
void showY();
}
class Parent {
void showParent() {
System.out.println("Parent class method");
}
}
class Child extends Parent implements X, Y {
public void showX() {
System.out.println("Interface X method");
}
public void showY() {
System.out.println("Interface Y method");
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
c.showParent();
c.showX();
c.showY();
}
}
Output:
Parent class method
Interface X method
Interface Y method
4. Summary of Inheritance Types
| TypeDescription | |
| Single | One child inherits from one parent |
| Multilevel | Chain of inheritance (grandparent → parent → child) |
| Hierarchical | Multiple children inherit from one parent |
| Multiple (via Interface) | One class implements multiple interfaces |
| Hybrid (Combination) | Combination of above types using classes and interfaces |
Key Points:
- Promotes code reuse and modularity
- Avoids redundancy
- Method overriding works with inheritance