What is Class Inheritance in Python & Its Types

Inheritance is one of the fundamental aspects of Object-Oriented Programming (OOP) that enables a class to inherit characteristics and behavior from another class. It facilitates code reusability, improves modularity, and encourages a disciplined approach to software development. Through inheritance, programmers can develop a parent class containing common functionalities and specialize it into subclass forms without having to recreate code.
Python’s inheritance is flexible and supports single, multiple, multilevel, hierarchical, and hybrid inheritance, thus making it a great tool for creating scalable applications. Knowing how inheritance works is important for developing maintainable and efficient object-oriented systems.
In this blog, we will discuss various types of inheritance, method overriding, and the usage of the super() function with examples.
Basics of Inheritance
In Python, a class can inherit from another class by specifying the parent class inside parentheses. Let’s see how this works with an example.
class Creature:
def __init__(self, title):
self.name = title
def produce_sound(self):
return "Some generic sound"
# Subclass inheriting from Creature
class Canine(Creature):
def produce_sound(self): # Overrides the method from Creature class
return "Bark Bark"
# Creating an instance of Canine
pet = Canine("Buddy")
print(pet.name) # Output: Buddy
print(pet.produce_sound()) # Output: Bark Bark
Here, the Dog class inherits the name attribute from the Animal class and overrides the make_sound() method.
Types of Inheritance
Python supports multiple types of inheritance:
1. Single Inheritance
A child class inherits from only a single parent class.
class WingedCreature:
def soar(self):
return "Yes, I can soar"
class SmallBird(WingedCreature):
pass
bird = SmallBird()
print(bird.soar()) # Output: Yes, I can soar
2. Multiple Inheritance
A subclass can inherit attributes and behaviors from multiple parent classes, so it can take on characteristics from multiple sources.
class FurryAnimal:
def fur_type(self):
return "Has a coat of fur"
class VocalCreature:
def sound_type(self):
return "Produces distinct sounds"
class HybridAnimal(FurryAnimal, VocalCreature):
pass
creature = HybridAnimal()
print(creature.fur_type()) # Output: Has a coat of fur
3. Multilevel Inheritance
A child class inherits from a parent class, and another child class inherits from it.
class LivingBeing:
def status(self):
return "Breathing and active"
class Animal(LivingBeing):
pass
class Dog(Animal):
pass
dog = Dog()
print(dog.status()) # Output: Breathing and active
4. Hierarchical Inheritance
Multiple child classes inherit from the same parent class, following a shared inheritance structure
class Vehicle:
def type(self):
return "Vehicle"
class Car(Vehicle):
pass
class Bike(Vehicle):
pass
car = Car()
bike = Bike()
print(car.type()) # Output: Vehicle
print(bike.type()) # Output: Vehicle
5. Hybrid Inheritance
Hybrid inheritance is a combination of two or more types of inheritance (single, multiple, hierarchical, or multilevel) in a single structure. This allows for complex class hierarchies but requires careful management of method resolution order (MRO).
class PowerSource:
def type(self):
return "Powered by an engine"
class Transport:
def movement(self):
return "Capable of motion"
class Automobile(Transport, PowerSource): # Multiple Inheritance
pass
class ElectricAutomobile(Automobile): # Multilevel Inheritance
def battery(self):
return "Operates using a battery"
electric_vehicle = ElectricAutomobile()
print(electric_vehicle.movement()) # Output: Capable of motion
print(electric_vehicle.type()) # Output: Powered by an engine
print(electric_vehicle.battery()) # Output: Operates using a battery
Method Overriding
A child class can redefine a method from the parent class.
class BaseClass:
def display(self):
return "Base class method"
class SubClass(BaseClass):
def display(self): # Overriding parent method
return "Subclass method"
obj = SubClass()
Using super() to Access Parent Methods
The super() function allows calling methods from the parent class inside the child class.
class BaseEntity:
def __init__(self, title):
self.entity_name = title
def display(self):
return f"Title: {self.entity_name}"
class DerivedEntity(BaseEntity):
def __init__(self, title, years):
super().__init__(title) # Calling the parent init method
self.entity_age = years
def display(self):
return f"{super().display()}, Duration: {self.entity_age} years"
obj = DerivedEntity("Project Alpha", 5)
print(obj.display()) # Output: Title: Project Alpha, Duration: 5 years
Conclusion
Inheritance is a strong Python feature that allows for code reuse, modularity, and maintainability. By inheriting properties and methods from the parent class, child classes can extend or override functionalities to achieve more efficient and scalable software development.
Knowing various forms of inheritance—i.e., single, multiple, multilevel, hierarchical, and hybrid inheritance—is crucial to developing well-designed object-oriented applications. Method overriding and the application of super() also allow child classes to extend inherited behavior while still taking advantage of the functionality of their parent classes.
By becoming proficient in Python inheritance, programmers can develop more flexible, reusable, and maintainable codebases, allowing their applications to be easier to extend and accommodate future needs.