Inheritance in Java: Types, Examples, and Explanation

Inheritance in Java

Inheritance is one of the core concepts of Object-Oriented Programming (OOP).
It allows one class to inherit the state (variables) and behavior (methods) of another class. 
In simple words, inheritance promotes code reuse and establishes a parent–child relationship between classes.

Definition

Inheritance is a mechanism where one class acquires the properties and methods of another class
The class that provides data is called the Superclass (Parent class)
The class that receives data is called the Subclass (Child class)
In Java, inheritance is implemented using the extends keyword

🌍 Real-Life Example

Family Inheritance
A child inherits eye color, height, and surname from parents
Parents → Superclass
Child → Subclass
Similarly, a subclass inherits variables and methods from its superclass.
🔹 Syntax of Inheritance
class SubClass extends SuperClass {
    // subclass members
}

🔑 Important Points

A subclass consumes the state and behavior of the superclass
A superclass provides state and behavior
In Java, every class indirectly inherits from the Object class
Object class is an inbuilt class in the Java library
🧠 Example Code
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();
        d.bark();
    }
}

🔄 Types of Inheritance in Java

Java supports four main types of inheritance:

1. Single Inheritance
✅ Definition

In single inheritance, one subclass inherits from one superclass.

🌍 Real-Life Example

One child inherits properties from one parent.

class A {
    void show() {
        System.out.println("Class A");
    }
}
class B extends A {
}

2. Multilevel Inheritance
✅ Definition

In multilevel inheritance, a class is derived from another subclass.
A subclass:
Acts as a subclass for its parent
Acts as a superclass for its child

🌍 Real-Life Example

Grandparent → Parent → Child
class A {
    void display() {
        System.out.println("Class A");
    }
}
class B extends A {
}
class C extends B {
}

3. Multiple Inheritance (Not Supported by Classes)

❌ Important Note
Java does not support multiple inheritance using classes to avoid ambiguity problems.
However, it supports multiple inheritance using interfaces.

🌍 Real-Life Example

A person can be a student and an employee at the same time.
interface A {
    void show();
}
interface B {
    void display();
}
class C implements A, B {
    public void show() {}
    public void display() {}
}

4. Hierarchical Inheritance
✅ Definition

In hierarchical inheritance, multiple subclasses inherit from a single superclass.

🌍 Real-Life Example

One parent with multiple children.
class Vehicle {
    void move() {
        System.out.println("Vehicle moves");
    }
}
class Car extends Vehicle {
}
class Bike extends Vehicle {
}

Comments

Post a Comment

Popular posts from this blog

History of Java Programming Language | Features, Origin & Uses

Java Programming Features Every Beginner Should Know