Inheritance and Polymorphism

Dive deeper into inheritance, its benefits, and different types (single, multiple, multilevel). Understand polymorphism (method overriding and overloading) and its applications.


Introduction to Inheritance in Java

Overview of Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (called a subclass or child class) to inherit properties and behaviors from another class (called a superclass or parent class). This creates an "is-a" relationship, meaning a subclass *is a* more specific type of the superclass. For example, a `Dog` *is a* `Animal`. Inheritance promotes code reuse and organization, making it easier to build complex and maintainable software.

Benefits of Inheritance

Code Reusability

One of the primary benefits of inheritance is code reusability. Instead of rewriting similar code in multiple classes, a subclass can inherit the existing code from a superclass. This reduces redundancy and makes the code more concise and easier to manage.

class Animal {
   String name;
   public void eat() {
       System.out.println("Animal is eating.");
   }
}

class Dog extends Animal {
   public void bark() {
       System.out.println("Dog is barking.");
   }
}

public class Main {
   public static void main(String[] args) {
       Dog myDog = new Dog();
       myDog.name = "Buddy"; // Accessing inherited property
       myDog.eat(); // Accessing inherited method
       myDog.bark(); // Accessing method specific to Dog
   }
}

Maintainability

Inheritance improves maintainability by centralizing common code in a superclass. If a change needs to be made to a shared behavior, it only needs to be modified in the superclass, and all subclasses will automatically inherit the update. This reduces the risk of introducing inconsistencies and errors when making changes.

Extensibility

Inheritance makes it easier to extend the functionality of a system. New subclasses can be created that inherit the existing behavior of the superclass and add or override specific behaviors to meet new requirements. This allows for adding new features without modifying existing code, reducing the risk of breaking existing functionality. This adheres to the Open/Closed Principle (a core principle in object-oriented design).

Real-World Analogies

Vehicles

Consider the concept of vehicles. A `Vehicle` can be a superclass. Subclasses could include `Car`, `Truck`, and `Motorcycle`. All vehicles have common properties like `speed` and `color`, and common methods like `accelerate()` and `brake()`. Each subclass then adds its own unique properties and methods, such as `numberOfDoors` for `Car` or `cargoCapacity` for `Truck`.

Employees

Another example is employees in a company. `Employee` can be the superclass. Subclasses could be `Manager`, `Developer`, and `Salesperson`. All employees share common attributes like `name`, `employeeId`, and `salary`, and common methods like `calculatePay()`. Each subclass can have its own specific attributes and methods, such as `numberOfDirectReports` for `Manager` or `programmingLanguage` for `Developer`.