Launch your tech mastery with usβ€”your coding journey starts now!
Course Content
Array Handling
0/1
String Handling
0/1
Wrapper Classes
0/1
Collections in Java
0/1
Packages
0/1
File Handling
0/1
Multithreading
0/1
Java Networking
0/1
Core Java

🌳 Inheritance in Java – Reusing Code Like a Pro!

In Object-Oriented Programming (OOP), Inheritance is like a family tree πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦ β€” where child classes inherit qualities from their parent class.


🌟 What is Inheritance?

πŸ‘‰ Inheritance is the mechanism in Java where one class (child/subclass) acquires the properties and behaviors (fields & methods) of another class (parent/superclass).

It helps in code reusability and establishes a natural hierarchy between classes.


🏑 Real-World Analogy

Think about a Car πŸš—:

  • A general Car (parent class) has properties like speed, fuel, wheels.

  • A SportsCar (child class) inherits all of these, but can also add its own features like turbo boost.

So, the child doesn’t need to redefine everything β€” it just reuses and adds more!

Terms used in Inheritance:

  • Class:Β A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
  • Sub Class/Child Class:Β Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
  • Super Class/Parent Class:Β Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
  • Reusability:Β As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.

⚑ Types of Inheritance in Java

πŸ”Ή Single Inheritance β†’ One class inherits from another.
πŸ”Ή Multilevel Inheritance β†’ A class inherits from a class which inherits from another.
πŸ”Ή Hierarchical Inheritance β†’ Multiple classes inherit from one parent.

❌ Multiple Inheritance (two classes as parents) is not supported directly in Java (to avoid ambiguity), but achieved via interfaces.


πŸ–₯️ Java Example: Inheritance in Action

// Parent class
class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

// Child class
class Dog extends Animal {
    void bark() {
        System.out.println("The dog barks.");
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.eat();   // Inherited from Animal
        d.bark();  // Defined in Dog
    }
}

βœ… Output:

This animal eats food.  
The dog barks.  

🎯 Benefits of Inheritance

  • πŸ”„ Code Reusability β†’ Write once, reuse everywhere.

  • πŸ— Extensibility β†’ Add new features without touching existing code.

  • 🧩 Organization β†’ Creates a clear class hierarchy.

  • πŸš€ Faster Development β†’ Less code duplication = faster coding.


✨ Quick Summary

πŸ‘‰ Inheritance = Parent β†’ Child relationship in OOP.
The child class inherits methods & variables from the parent, making code cleaner, shorter, and more powerful.

πŸ’‘ Pro Tip: Always use inheritance when there is an β€œis-a” relationship.
Example: Dog is an Animal βœ… | Car is a Vehicle βœ… | Keyboard is a Computer ❌ (wrong, that’s composition).

Generated image

Β