Python

In Python, Inheritance is a fundamental pillar of Object-Oriented Programming (OOP) that allows a class (called a Child or Derived class) to acquire the properties and behaviors (attributes and methods) of another class (called a Parent or Base class).

1. Theoretical Overview

The Concept of "Is-A" Relationship

Inheritance represents an "is-a" relationship. For example, a Car is-a Vehicle, and a Manager is-a Employee. This structure allows you to build a hierarchy of classes that move from general to specific.

Key Benefits

  • Code Reusability: You don't have to rewrite the same logic for every new class. You write it once in the Parent class and reuse it in all Child classes.

  • Extensibility: You can add new features to a Child class without modifying the Parent class.

  • Maintainability: If a bug exists in the shared logic, you only need to fix it in the Parent class.

The super() Function

The super() function is used to give the child class access to methods and properties of the parent class. It is most commonly used inside the __init__ method to ensure the parent class is initialized properly.

2. Types of Inheritance in Python

  1. Single Inheritance: A child class inherits from one parent class.

  2. Multiple Inheritance: A child class inherits from more than one parent class.

  3. Multilevel Inheritance: A child class inherits from a parent, which in turn inherits from another class (Grandparent $\rightarrow$ Parent $\rightarrow$ Child).

  4. Hierarchical Inheritance: Multiple child classes inherit from a single parent class.

3. Code Implementation

A. Single Inheritance

The simplest form where one class inherits from another.

Python
# Parent Class
class Animal:
    def __init__(self, name):
        self.name = name

    def breathe(self):
        print(f"{self.name} is breathing.")

# Child Class
class Dog(Animal):
    def bark(self):
        print(f"{self.name} says Woof!")

# Usage
my_dog = Dog("Buddy")
my_dog.breathe() # Inherited method
my_dog.bark()    # Child's own method

B. Using super() and Overriding

Sometimes, a child class needs to perform the parent's initialization plus its own.

Python
class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def work(self):
        print(f"{self.name} is working.")

class Developer(Employee):
    def __init__(self, name, salary, language):
        # Use super() to call the Parent constructor
        super().__init__(name, salary)
        self.language = language

    # Method Overriding: Changing parent behavior
    def work(self):
        print(f"{self.name} is coding in {self.language}.")

dev = Developer("Ankit", 80000, "Python")
dev.work() 

4. Summary Table

TermDefinition
Base ClassThe class being inherited from (Parent).
Derived ClassThe class that inherits from the Base class (Child).
Method OverridingRedefining a parent method inside the child class.
super()Built-in function to call parent methods.
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now