Inheritance in Python
In Python, inheritance is a fundamental concept in Object-Oriented Programming (OOP). It allows a class (called the child class) to inherit the attributes and methods of another class (called the parent class). This enables code reuse and helps to build a hierarchy of classes.
What is Inheritance?
Inheritance allows one class to inherit the properties and behaviors (methods) of another class. The new class is called the derived class or child class, and the class it inherits from is the base class or parent class. The child class can add new properties or methods, or it can modify the behavior of the parent class's methods.
Creating a Simple Inheritance Example
Here is a simple example of how inheritance works:
# Parent class (Animal) class Animal: def __init__(self, name): self.name = name def speak(self): return f"{self.name} makes a sound." # Child class (Dog) inheriting from Animal class Dog(Animal): def speak(self): return f"{self.name} barks." # Example Usage dog = Dog("Buddy") print(dog.speak()) # Output: Buddy barks.
Why Use Inheritance?
Inheritance helps in code reuse and allows us to create a modular structure. It is especially useful when multiple classes have similar behavior or attributes. Instead of rewriting code for each class, you can define common behavior in a parent class and have child classes inherit it.
Overriding Methods in the Child Class
A child class can override the methods of the parent class to provide its own implementation. For example:
# Parent class (Animal) class Animal: def __init__(self, name): self.name = name def speak(self): return f"{self.name} makes a sound." # Child class (Cat) overriding the speak method class Cat(Animal): def speak(self): return f"{self.name} meows." cat = Cat("Whiskers") print(cat.speak()) # Output: Whiskers meows.
Using super()
to Access Parent Class Methods
Even if the child class overrides a method, you can call the parent class's version of the method using the
super()
function. This allows the child class to use the parent class's functionality along
with its own.
class Animal: def __init__(self, name): self.name = name def speak(self): return f"{self.name} makes a sound." class Dog(Animal): def speak(self): return f"{self.name} barks." def parent_speak(self): return super().speak() dog = Dog("Buddy") print(dog.speak()) # Output: Buddy barks. print(dog.parent_speak()) # Output: Buddy makes a sound.
Multiple Inheritance
Python supports multiple inheritance, which means a class can inherit from more than one parent class. For example:
class Animal: def __init__(self, name): self.name = name class Mammal: def is_mammal(self): return True class Dog(Animal, Mammal): def speak(self): return f"{self.name} barks." dog = Dog("Buddy") print(dog.speak()) # Output: Buddy barks. print(dog.is_mammal()) # Output: True
Summary of Inheritance
- Inheritance allows you to reuse code by creating a new class based on an existing class.
- Child classes can override parent class methods to provide custom behavior.
super()
lets you call parent class methods even if they are overridden in the child class.- Python supports multiple inheritance, allowing a class to inherit from multiple parent classes.