Inheritance in Java

What is Inheritance?

**Inheritance** is one of the fundamental concepts of **Object-Oriented Programming (OOP)** in Java. It allows a class to inherit **properties and methods** from another class, promoting **code reusability**.

Why Use Inheritance?

  • Eliminates code duplication.
  • Makes code more organized and manageable.
  • Supports the **"is-a"** relationship between classes.
  • Enables method overriding for **polymorphism**.

Types of Inheritance in Java

  • Single Inheritance: A class inherits from one superclass.
  • Multilevel Inheritance: A class inherits from another derived class.
  • Hierarchical Inheritance: Multiple classes inherit from a single superclass.

1. Single Inheritance

In this type, a **child class** inherits from a **single parent class**.

class Animal {
    void makeSound() {
        System.out.println("Animal makes a sound");
    }
}

// Dog class inherits from Animal
class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks");
    }
}

public class SingleInheritance {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.makeSound(); // Inherited method
        dog.bark(); // Own method
    }
}

2. Multilevel Inheritance

In **multilevel inheritance**, a class inherits from another derived class.

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

// Dog inherits from Animal
class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks.");
    }
}

// Puppy inherits from Dog
class Puppy extends Dog {
    void weep() {
        System.out.println("Puppy weeps.");
    }
}

public class MultilevelInheritance {
    public static void main(String[] args) {
        Puppy pup = new Puppy();
        pup.eat(); // Inherited from Animal
        pup.bark(); // Inherited from Dog
        pup.weep(); // Own method
    }
}

3. Hierarchical Inheritance

In **hierarchical inheritance**, multiple child classes inherit from the same parent class.

class Animal {
    void makeSound() {
        System.out.println("Animal makes a sound.");
    }
}

// Cat inherits from Animal
class Cat extends Animal {
    void meow() {
        System.out.println("Cat meows.");
    }
}

// Dog also inherits from Animal
class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks.");
    }
}

public class HierarchicalInheritance {
    public static void main(String[] args) {
        Cat cat = new Cat();
        Dog dog = new Dog();

        cat.makeSound(); // Inherited method
        cat.meow(); // Own method

        dog.makeSound(); // Inherited method
        dog.bark(); // Own method
    }
}

Method Overriding in Inheritance

Method overriding allows a **child class** to provide a **specific implementation** of a method that is already defined in its parent class.

class Animal {
    void makeSound() {
        System.out.println("Animal makes a sound.");
    }
}

// Dog class overrides the method
class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Dog barks.");
    }
}

public class MethodOverriding {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        myDog.makeSound(); // Calls the overridden method
    }
}

Super Keyword in Inheritance

The super keyword is used to **call the parent class constructor or methods** inside a child class.

class Animal {
    Animal() {
        System.out.println("Animal Constructor");
    }
}

class Dog extends Animal {
    Dog() {
        super(); // Calls the Animal constructor
        System.out.println("Dog Constructor");
    }
}

public class SuperKeyword {
    public static void main(String[] args) {
        Dog d = new Dog(); // Calls both constructors
    }
}

Difference Between Inheritance and Composition

Feature Inheritance Composition
Relationship "is-a" relationship (Dog is an Animal) "has-a" relationship (Car has an Engine)
Flexibility Less flexible (tight coupling) More flexible (loose coupling)
Code Reusability Yes, but increases dependency Yes, promotes modular design

Conclusion

Inheritance is a key concept in **Object-Oriented Programming** that helps in **code reusability and organization.** However, it should be used wisely to avoid **unnecessary complexity and tight coupling.**

📌 Next Topic: Polymorphism in Java