Inheritance

Carson West

Python 1 Home

Inheritance Inheritance is a powerful mechanism in object-oriented programming (OOP) that allows you to create new classes (child classes or subclasses) based on existing classes (parent classes or superclasses). The child class inherits the attributes (variables) and methods (functions) of the parent class, and can also add its own unique attributes and methods, or override existing ones.

Benefits of Inheritance:

Example:

class Animal:  # Parent class
    def __init__(self, name):
        self.name = name

    def speak(self):
        print("Generic animal sound")

class Dog(Animal):  # Child class inheriting from Animal
    def speak(self):
        print("Woof!")

class Cat(Animal): # Child class inheriting from Animal
    def speak(self):
        print("Meow!")

my_dog = Dog("Buddy")
my_cat = Cat("Whiskers")

my_dog.speak()  # Output: Woof!
my_cat.speak()  # Output: Meow!
print(my_dog.name) # Output: Buddy (inherited from Animal)

Types of Inheritance:

Method Overriding: As shown in the example, the Dog and Cat classes override the speak() method from the Animal class. This allows child classes to provide their own specific implementations.

Super() Function: The super() function is used to call methods of the parent class from within the child class. This is particularly useful when you want to extend, rather than replace, the functionality of a parent class method.

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

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name) # Calls the parent class's __init__ method
        self.breed = breed

    def speak(self):
        print(f"Woof! My name is {self.name}, and I'm a {self.breed}.")

Classes and Objects Method Resolution Order (MRO)