In Python, inheritance is a way to create a new class that is a modified version of an existing class. The new class is called the subclass, and the existing class is the superclass. The subclass inherits the member variables and methods of the superclass, but can also have its own unique member variables and methods.

For example, consider the following code:

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

    def make_sound(self):
        print("Some generic animal sound")

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name, species="Dog")
        self.breed = breed

    def make_sound(self):
        print("Woof!")

In the above example, the “Animal” class has a member variable “name” and a member variable “species”, as well as a “make_sound” method. The “Dog” class is a subclass of the “Animal” class, which means it inherits the member variables and methods of the “Animal” class. The “Dog” class also has its own unique member variable “breed” and a modified version of the “make_sound” method.

To create an object from the “Dog” class, you can use the following code:

dog1 = Dog("Fido", "Labrador")

To access the inherited member variables and methods of the “Dog” object, you can use the dot operator as you would with any other object. For example, to access the “name” member variable of the “dog1” object, you can use the following code:

print(dog1.name)

To call the inherited “make_sound” method of the “dog1” object, you can use the following code:

dog1.make_sound()

In the above example, the “make_sound” method of the “dog1” object is called, which prints “Woof!” because it is the modified version of the “make_sound” method in the “Dog” class.

Conclusion

Inheritance is a powerful concept in Python that allows you to create more specialized classes from more general classes, and to reuse code in a efficient and organized manner.

Exercises

Here are some exercises with solutions to help you practice what you just learned:

How do you create a subclass in Python?

To create a subclass in Python, you use the name of the superclass in parentheses after the name of the subclass. For example, consider the following code:

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

class Dog(Animal):
    pass

In the above example, the “Animal” class is the superclass and the “Dog” class is the subclass. The “Dog” class inherits the member variables and methods of the “Animal” class.

How do you access the inherited member variables and methods of a subclass in Python?

To access the inherited member variables and methods of a subclass in Python, you use the dot operator followed by the name of the member variable or method. For example, consider the following code:

class Animal:
    def __init__(self, name, species):
        self.name = name
        self.species = species
    def make_sound(self):
        print("Some generic animal sound")
class Dog(Animal):
    def init(self, name, breed):
        super().init(name, species="Dog")
        self.breed = breed
    def make_sound(self):
        print("Woof!")

dog1 = Dog("Fido", "Labrador")
print(dog1.name)
dog1.make_sound()

In the above example, the “name” member variable and the “make_sound” method of the “dog1” object are accessed using the dot operator.

How do you call the constructor of the superclass from a subclass in Python?

To call the constructor of the superclass from a subclass in Python, you use the “super” function followed by the dot operator and the name of the constructor method. For example, consider the following code:

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

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name, species="Dog")
        self.breed = breed

In the above example, the constructor of the “Animal” class is called from the “Dog” class using the “super” function and the dot operator.

How do you override an inherited method in a subclass in Python?

To override an inherited method in a subclass in Python, you define a new version of the method with the same name in the subclass. For example, consider the following code:

class Animal:
    def make_sound(self):
        print("Some generic animal sound")

class Dog(Animal):
    def make_sound(self):
        print("Woof!")

In the above example, the “make_sound” method of the “Dog” class overrides the inherited “make_sound” method of the “Animal” class.

How do you access the overridden method of a superclass from a subclass in Python?

To access the overridden method of a superclass from a subclass in Python, you use the “super” function followed by the dot operator and the name of the method. For example, consider the following code:

class Animal:
    def make_sound(self):
        print("Some generic animal sound")

class Dog(Animal):
    def make_sound(self):
        print("Woof!")
        super().make_sound()

dog1 = Dog()
dog1.make_sound()

In the above example, the “make_sound” method of the “Dog” class calls the overridden “make_sound” method of the “Animal” class using the “super” function and the dot operator. This results in the output “Woof!” followed by “Some generic animal sound”.