Python Inheritance

In Python, inheritance allows a child class to reuse and extend the functionality of a parent class, making code more modular and reducing duplication. You can define a base class with attributes and methods, then create derived classes that inherit and add their own behavior.

Key Concepts of Inheritance

Let us first see the key concepts:

  • Parent/Base Class: The class whose properties and methods are inherited.
  • Child/Derived Class: The class that inherits from the parent.
  • Syntax:
class Parent:
    # attributes and methods

class Child(Parent):
    # additional attributes and methods

or

# defining a superclass
class superclass:
    # attributes and methods definition

# inheritance
# defining a sub class
class subclass(super_class):
    # attributes and methods of superclass
    # attributes and methods of subclass

Above, we are inheriting the subclass from superclass.

Why Use Inheritance

  • Code Reusability: Avoid rewriting common methods.
  • Extensibility: Add new features in child classes.
  • Organization: Clear hierarchy of classes.

Types of Inheritance in Python

The following are the types of Inheritance in Python:

Single Inheritance

A child class inherits from one parent class.

class Animal:
    def __init__(self, name):
        self.name = name
    
    def info(self):
        print("Animal name:", self.name)

class Dog(Animal):  # Dog inherits from Animal
    def sound(self):
        print(self.name, "barks")

d = Dog("Bradman")
d.info()   # Inherited method
d.sound()  # Child-specific method

Output

Animal name: Bradman
Brandman barks

Here, Dog inherits the info() method from Animal and adds its own sound() method.

Here’s the representation for your Single Inheritance example featuring Animal and Dog classes with Buddy as the instance:
Single Inheritance in Python

Multiple Inheritance

A child class inherits from more than one parent class.

class Father:
    def skills(self):
        print("Gardening, Programming")

class Mother:
    def skills(self):
        print("Cooking, Art")

class Child(Father, Mother):
    def skills(self):
        Father.skills(self)
        Mother.skills(self)
        print("Dancing, Sports")

c = Child()
c.skills()

Output

Gardening, Programming
Cooking, Art
Dancing, Sports

The Child class inherits from both Father and Mother.

Here is the representation for Multiple Inheritance using the Father, Mother, and Child classes:

Multiple Inheritance in Python

Multilevel Inheritance

Inheritance occurs across multiple levels (grandparent → parent → child).

class Grandparent:
    def heritage(self):
        print("Family traditions")

class Parent(Grandparent):
    def responsibility(self):
        print("Providing guidance")

class Child(Parent):
    def play(self):
        print("Playing games")

c = Child()
c.heritage()
c.responsibility()
c.play()

Output

Family traditions
Providing guidance
Playing games

The Child inherits from Parent, which itself inherits from Grandparent.

Here is the representation for Multilevel Inheritance showcasing the Grandparent, Parent, and Child classes:

Multilevel Inheritance in Python

Hierarchical Inheritance

Multiple child classes inherit from the same parent class.

class Animal:
    def sound(self):
        print("Some generic sound")

class Dog(Animal):
    def sound(self):
        print("Bark")

class Cat(Animal):
    def sound(self):
        print("Meow")

class Cow(Animal):
    def sound(self):
        print("Moo")

# Testing
d = Dog()
c = Cat()
cw = Cow()

d.sound()
c.sound()
cw.sound()

Output

Bark
Meow
Moo

All child classes (Dog, Cat, Cow) inherit from the same parent Animal, but override the sound() method differently.

Here is the representation:

Hierarchical Inheritance in Python

Hybrid Inheritance

A combination of two or more types of inheritance.

class LivingBeing:
    def breathe(self):
        print("Breathing...")

class Animal(LivingBeing):
    def move(self):
        print("Animal moves")

class Bird(LivingBeing):
    def move(self):
        print("Bird flies")

class Bat(Animal, Bird):  # Hybrid: multiple + hierarchical
    def special(self):
        print("Bat uses echolocation")

# Testing
b = Bat()
b.breathe()   # From LivingBeing
b.move()      # Resolves using Method Resolution Order (MRO)
b.special()

Output

Breathing...
Animal moves
Bat uses echolocation

Bat inherits from both Animal and Bird, while they themselves inherit from LivingBeing. This creates a hybrid structure.

Here is the representation:

Hybrid Inheritance in Python

Note: Python uses Method Resolution Order (MRO) to decide which parent’s method to call when there’s ambiguity.


If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Python Polymorphism
Python Encapsulation
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment