Inheritance is one of the key features of object-oriented programming (OOP). It allows one class (child class) to inherit attributes and methods from another class (parent class). This promotes code reuse and can make your programs more modular and easier to maintain.
In single inheritance, a child class inherits from a single parent class.
Example:
# Parent class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError("Subclass must implement abstract method")
# Child class
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
# Creating an instance of Dog
dog = Dog("Buddy")
print(dog.speak()) # Output: Buddy says Woof!
In multiple inheritance, a child class inherits from more than one parent class.
Example:
# Parent class 1
class Walker:
def walk(self):
return "Walking"
# Parent class 2
class Swimmer:
def swim(self):
return "Swimming"
# Child class
class Amphibian(Walker, Swimmer):
pass
# Creating an instance of Amphibian
amphibian = Amphibian()
print(amphibian.walk()) # Output: Walking
print(amphibian.swim()) # Output: Swimming
In multilevel inheritance, a child class inherits from a parent class, which in turn inherits from another parent class.
Example:
# Parent class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError("Subclass must implement abstract method")
# Intermediate class
class Mammal(Animal):
def feed_young(self):
return f"{self.name} is feeding its young"
# Child class
class Dog(Mammal):
def speak(self):
return f"{self.name} says Woof!"
# Creating an instance of Dog
dog = Dog("Buddy")
print(dog.speak()) # Output: Buddy says Woof!
print(dog.feed_young()) # Output: Buddy is feeding its young
In hierarchical inheritance, multiple child classes inherit from a single parent class.
Example:
# Parent class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError("Subclass must implement abstract method")
# Child class 1
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
# Child class 2
class Cat(Animal):
def speak(self):
return f"{self.name} says Meow!"
# Creating instances of Dog and Cat
dog = Dog("Buddy")
cat = Cat("Whiskers")
print(dog.speak()) # Output: Buddy says Woof!
print(cat.speak()) # Output: Whiskers says Meow!
Hybrid inheritance is a combination of two or more types of inheritance. It can involve any combination of single, multiple, multilevel, and hierarchical inheritance.
Example:
# Parent class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError("Subclass must implement abstract method")
# Intermediate class 1
class Walker(Animal):
def walk(self):
return "Walking"
# Intermediate class 2
class Swimmer(Animal):
def swim(self):
return "Swimming"
# Child class
class Amphibian(Walker, Swimmer):
def speak(self):
return f"{self.name} can walk and swim"
# Creating an instance of Amphibian
amphibian = Amphibian("Froggy")
print(amphibian.speak()) # Output: Froggy can walk and swim
print(amphibian.walk()) # Output: Walking
print(amphibian.swim()) # Output: Swimming
A child class can override or extend the functionality of methods inherited from a parent class.
Example:
class Animal:
def speak(self):
return "Animal sound"
class Dog(Animal):
def speak(self):
return "Woof!"
# Creating instances
animal = Animal()
dog = Dog()
print(animal.speak()) # Output: Animal sound
print(dog.speak()) # Output: Woof!