Single inheritance refers to the scenario where a class (child or derived class) inherits from one and only one base class (parent class). This forms a simple, straightforward hierarchy.
Single inheritance helps to:
Let’s look at a practical example to understand single inheritance better.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError("Subclass must implement abstract method")
def move(self):
print(f"{self.name} is moving")
class Dog(Animal):
def speak(self):
return f"{self.name} says Woof!"
if __name__ == "__main__":
my_dog = Dog("Buddy")
print(my_dog.speak()) # Output: Buddy says Woof!
my_dog.move() # Output: Buddy is moving
Animal:
__init__ method initializes the name attribute.Dog:
name attribute and move method from the Animal class.speak method, providing the functionality specific to Dog.