Skip to main content

Inheritance and Polymorphism in Object-Oriented Programming

Introduction

Inheritance and polymorphism are two cornerstone concepts of object-oriented programming (OOP). They enable developers to create more modular, reusable, and flexible code. This guide covers these concepts in detail, explaining how they work and how they are implemented in languages like Java, C++, and Python.

What is Inheritance?

Inheritance allows a new class (called a subclass or derived class) to inherit the properties and methods of an existing class (called a superclass or base class). This helps in reducing code redundancy and promotes reusability.

Types of Inheritance

  1. Single Inheritance: A subclass inherits from one superclass.

    • Example: A class Dog inherits from the class Animal.
    class Animal {
    void eat() {
    System.out.println("This animal eats.");
    }
    }

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

    // Usage
    Dog myDog = new Dog();
    myDog.eat(); // Inherited from Animal
    myDog.bark(); // Defined in Dog
  2. Multiple Inheritance: A class inherits from more than one parent class (supported in C++ but not in Java). In Java, interfaces are used to achieve similar functionality.

  3. Multilevel Inheritance: A class inherits from another class, which itself inherits from a third class.

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

    class Mammal extends Animal {
    void walk() {
    System.out.println("This mammal walks.");
    }
    }

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

    // Usage
    Dog myDog = new Dog();
    myDog.eat(); // Inherited from Animal
    myDog.walk(); // Inherited from Mammal
    myDog.bark(); // Defined in Dog
  4. Hierarchical Inheritance: A single superclass is inherited by multiple subclasses.

  5. Hybrid Inheritance: A combination of more than one type of inheritance. It may involve using interfaces in languages like Java.

What is Polymorphism?

Polymorphism means "many forms." In OOP, it allows objects to be treated as instances of their parent class, enabling multiple implementations to share the same interface. There are two types of polymorphism:

Compile-Time Polymorphism (Method Overloading)

In compile-time polymorphism, a class can have more than one method with the same name but different parameter lists. This is called method overloading.

class MathOperations {
int add(int a, int b) {
return a + b;
}

int add(int a, int b, int c) {
return a + b + c;
}
}

// Usage
MathOperations mo = new MathOperations();
System.out.println(mo.add(5, 3)); // Calls the 2-parameter version
System.out.println(mo.add(5, 3, 7)); // Calls the 3-parameter version

Runtime Polymorphism (Method Overriding)

In runtime polymorphism, a subclass can provide its own implementation of a method that is already defined in its superclass. This is called method overriding.

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

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

// Usage
Animal myAnimal = new Dog();
myAnimal.makeSound(); // Output: Dog barks. (Polymorphism at runtime)

Summary

  • Inheritance allows a subclass to inherit properties and methods from a superclass, reducing code duplication.
  • Polymorphism enables one method to have different forms, either by method overloading or method overriding.
  • These OOP concepts make code more modular, reusable, and easier to maintain.