Python Inheritance

Inheritance is a core concept of Object-Oriented Programming (OOP) that allows one class (child class) to inherit attributes and methods from another class (parent class). Python supports single, multiple, multilevel, and hierarchical inheritance. This tutorial explains how inheritance works with examples.

Why Use Inheritance?

Inheritance allows you to reuse code by creating a new class from an existing class. The new class (child class) inherits the methods and attributes of the existing class (parent class), reducing code duplication and enhancing maintainability.

Types of Inheritance

  • Single Inheritance: A child class inherits from a single parent class.
  • Multiple Inheritance: A child class inherits from more than one parent class.
  • Multilevel Inheritance: A child class inherits from a parent class, which itself inherits from another class.
  • Hierarchical Inheritance: Multiple child classes inherit from the same parent class.

1. Single Inheritance

In single inheritance, the child class inherits from a single parent class.

Example:

# Single Inheritance
class Animal:
    def speak(self):
        return "Animal speaks"

class Dog(Animal):
    def bark(self):
        return "Dog barks"

# Create an object of Dog class
dog = Dog()
print(dog.speak())  # Output: Animal speaks
print(dog.bark())   # Output: Dog barks

Try It Now

2. Multiple Inheritance

In multiple inheritance, a child class inherits from more than one parent class.

Example:

# Multiple Inheritance
class Father:
    def skill_father(self):
        return "Father's skill"

class Mother:
    def skill_mother(self):
        return "Mother's skill"

class Child(Father, Mother):
    def skill_child(self):
        return "Child's skill"

# Create an object of Child class
child = Child()
print(child.skill_father())  # Output: Father's skill
print(child.skill_mother())  # Output: Mother's skill
print(child.skill_child())   # Output: Child's skill

Try It Now

3. Multilevel Inheritance

In multilevel inheritance, a class inherits from a child class, making a chain of inheritance.

Example:

# Multilevel Inheritance
class Grandparent:
    def grandparent_method(self):
        return "Grandparent method"

class Parent(Grandparent):
    def parent_method(self):
        return "Parent method"

class Child(Parent):
    def child_method(self):
        return "Child method"

# Create an object of Child class
child = Child()
print(child.grandparent_method())  # Output: Grandparent method
print(child.parent_method())       # Output: Parent method
print(child.child_method())        # Output: Child method

Try It Now

4. Hierarchical Inheritance

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

Example:

# Hierarchical Inheritance
class Vehicle:
    def vehicle_info(self):
        return "This is a vehicle"

class Car(Vehicle):
    def car_info(self):
        return "This is a car"

class Bike(Vehicle):
    def bike_info(self):
        return "This is a bike"

# Create objects
car = Car()
bike = Bike()
print(car.vehicle_info())  # Output: This is a vehicle
print(car.car_info())      # Output: This is a car
print(bike.vehicle_info()) # Output: This is a vehicle
print(bike.bike_info())    # Output: This is a bike

Try It Now

Method Overriding

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

Example:

class Animal:
    def speak(self):
        return "Animal speaks"

class Dog(Animal):
    def speak(self):  # Method overriding
        return "Dog barks"

dog = Dog()
print(dog.speak())  # Output: Dog barks

Try It Now

The super() Function

The super() function is used to call a method from the parent class. This is especially useful when overriding methods.

Example:

class Animal:
    def speak(self):
        return "Animal speaks"

class Dog(Animal):
    def speak(self):
        parent_message = super().speak()  # Call parent method
        return f"{parent_message} and Dog barks"

dog = Dog()
print(dog.speak())  # Output: Animal speaks and Dog barks

Try It Now

Conclusion

Inheritance is a powerful feature of Python that promotes code reuse and helps in creating hierarchical class structures. Understanding how different types of inheritance work is crucial for writing modular and maintainable Python programs. Keep practicing to master inheritance!