Understanding Java Inheritance: A Practical Example

Inheritance is a fundamental concept in object-oriented programming, enabling code reuse and establishing "is-a" relationships between classes. Java, being an object-oriented language, supports inheritance, allowing one class to acquire the properties and methods of another. Let's dive into a practical example to understand Java inheritance better.

Base Class: Animal
Let's start by creating a base class named Animal. This class will have common properties and behaviors for all animals.

```java public class Animal { private String name; public Animal(String name) { this.name = name; } public void makeSound() { System.out.println("The animal makes a sound"); } public String getName() { return name; } } ```
Derived Classes: Dog and Cat
Now, let's create two derived classes, Dog and Cat, that inherit from the Animal class. These classes will have their unique properties and behaviors while inheriting common ones from the Animal class.

Dog Class
The Dog class will have an additional property breed and override the makeSound method to provide a specific implementation.
```java public class Dog extends Animal { private String breed; public Dog(String name, String breed) { super(name); this.breed = breed; } @Override public void makeSound() { System.out.println("The dog says: Woof!"); } public String getBreed() { return breed; } } ```
Cat Class

The Cat class will also have an additional property color and override the makeSound method to provide a specific implementation.
```java public class Cat extends Animal { private String color; public Cat(String name, String color) { super(name); this.color = color; } @Override public void makeSound() { System.out.println("The cat says: Meow!"); } public String getColor() { return color; } } ```
Using Inheritance in Java
Now that we have our base and derived classes, let's see how we can use inheritance in Java.

```java public class Main { public static void main(String[] args) { Animal myAnimal = new Animal("Animal"); Dog myDog = new Dog("Buddy", "Labrador"); Cat myCat = new Cat("Whiskers", "Black"); myAnimal.makeSound(); // The animal makes a sound myDog.makeSound(); // The dog says: Woof! myCat.makeSound(); // The cat says: Meow! System.out.println(myDog.getBreed()); // Labrador System.out.println(myCat.getColor()); // Black } } ```
Inheritance Hierarchy and Method Resolution
When a method is called on an object, Java looks for the method in the following order:



















- In the current class (most specific class).
- In the superclass.
- In the superclass's superclass, and so on.
This order is known as the inheritance hierarchy or method resolution order (MRO).
Benefits of Inheritance in Java
Inheritance provides several benefits in Java, such as:
| Benefit | Description |
|---|---|
| Code Reusability | Inheritance allows common properties and methods to be shared among classes, reducing code duplication. |
| Polymorphism | Inheritance enables polymorphism, allowing objects of different classes to be treated as objects of a common superclass. |
| Easy Extensibility | Inheritance makes it easy to extend the functionality of a class by adding new properties and methods or overriding existing ones. |
Inheritance is a powerful feature in Java that promotes code reuse, extensibility, and polymorphism. By understanding and utilizing inheritance effectively, you can create more maintainable and efficient object-oriented software.