0
0
JavaConceptBeginner · 3 min read

What is Inheritance in Java: Explanation and Example

In Java, inheritance is a feature where one class (called a subclass) can inherit fields and methods from another class (called a superclass). This allows code reuse and helps create a relationship between classes, making programs easier to organize and maintain.
⚙️

How It Works

Inheritance in Java works like a family tree. Imagine a child inheriting traits from their parents, such as eye color or height. Similarly, a subclass inherits properties and behaviors (fields and methods) from its superclass. This means the subclass can use or modify what it inherits without rewriting the code.

For example, if you have a class Animal with a method makeSound(), a subclass Dog can inherit this method and use it directly or change it to make a dog-specific sound. This helps programmers avoid repeating code and makes it easier to add new features by building on existing classes.

💻

Example

This example shows a superclass Animal and a subclass Dog that inherits from Animal. The Dog class uses the inherited method and adds its own method.

java
class Animal {
    void makeSound() {
        System.out.println("Some generic animal sound");
    }
}

class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Bark");
    }
    void fetch() {
        System.out.println("Dog is fetching the ball");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Animal();
        myAnimal.makeSound();

        Dog myDog = new Dog();
        myDog.makeSound();  // Overridden method
        myDog.fetch();      // Dog-specific method
    }
}
Output
Some generic animal sound Bark Dog is fetching the ball
🎯

When to Use

Use inheritance when you have classes that share common features but also have their own unique behaviors. It helps organize code by creating a clear hierarchy, like grouping all animals under one Animal class and then specifying different types like Dog or Cat.

Real-world uses include:

  • Creating a base class for different types of vehicles, then extending it for cars, trucks, and motorcycles.
  • Building user interface components where a general Button class is extended by specialized buttons.
  • Modeling employees in a company where a general Employee class is extended by Manager or Developer classes.

Key Points

  • Inheritance allows a class to reuse code from another class.
  • The subclass inherits fields and methods from the superclass.
  • Subclasses can override methods to change behavior.
  • It helps organize code and reduce duplication.
  • Java supports single inheritance, meaning a class can extend only one superclass.

Key Takeaways

Inheritance lets a class reuse code from another class to avoid repetition.
A subclass inherits methods and fields from its superclass and can add or change them.
Use inheritance to model real-world relationships and organize code logically.
Java supports single inheritance, so each class can extend only one other class.
Overriding methods in subclasses allows customizing inherited behavior.