Types of Inheritance in Java

 

🔷 What is Inheritance in Java?

Inheritance is the process by which one class (child) acquires the properties and behaviors (fields and methods) of another class (parent).
It promotes code reusability and is a key feature of object-oriented programming.

🔑 Syntax:


class Parent { // parent members } class Child extends Parent { // child members }

✅ Types of Inheritance in Java:

TypeDescription
1. Single InheritanceOne class inherits from another.
2. Multilevel InheritanceA class inherits from a class which is already inherited from another class.
3. Hierarchical InheritanceMultiple classes inherit from a single parent class.
4. Hybrid InheritanceCombination of multiple types (achievable via interfaces in Java).

Note: Java does not support multiple inheritance with classes to avoid ambiguity (Diamond Problem). But it is allowed through interfaces.

🔹 1. Single Inheritance

A child class inherits directly from a parent class.

📌 Example:

class Animal {
void sound() { System.out.println("Animal makes sound"); } } 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.sound(); // inherited d.bark(); // own method } }

🔹 2. Multilevel Inheritance

A class inherits from a class which itself inherits from another class.

📌 Example:


class Animal { void eat() { System.out.println("Eating..."); } } class Dog extends Animal { void bark() { System.out.println("Barking..."); } } class Puppy extends Dog { void weep() { System.out.println("Weeping..."); } } public class Main { public static void main(String[] args) { Puppy p = new Puppy(); p.eat(); p.bark(); p.weep(); } }

🔹 3. Hierarchical Inheritance

Multiple classes inherit from a single parent class.

📌 Example:

class Animal {
void sound() { System.out.println("Animal sound"); } } 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(); Cat c = new Cat(); d.sound(); c.sound(); } }

🔹 4. Hybrid Inheritance (via Interfaces)

Java does not allow multiple inheritance with classes but allows it through interfaces, achieving hybrid inheritance.

📌 Example:


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(); } }

🚫 Why Java Does Not Support Multiple Inheritance with Classes?

To avoid ambiguity caused by the Diamond Problem.

🔻 Example of Diamond Problem (Not allowed in Java):


A / \ B C \ / D

If both B and C inherit a method from A, and D inherits from both B and C, which method should D use? To prevent this confusion, Java restricts multiple inheritance of classes, but allows it via interfaces.


🧠 Summary Table:

TypeSupports in JavaExample Components
Single Inheritance                    ✅ Yes        One child, one parent
Multilevel Inheritance✅ Yes        Grandparent → Parent → Child
Hierarchical Inheritance✅ Yes        One parent, many children
Multiple Inheritance❌ (with classes)
✅ (with interfaces)
        Interfaces only
Hybrid Inheritance✅ via interfaces        Mix of inheritance types

Comments

Popular posts from this blog

Object Oriented Programming PBCST304 KTU BTech CS Semester 3 2024 Scheme - Dr Binu V P

Introduction to Java Programming

Inheritance and Polymorphism