leafleafleafDocy banner shape 01Docy banner shape 02Man illustrationFlower illustration

Inheritance of Python classes

Estimated reading: 4 minutes 14 views

Every time you write a class, you don’t have to write code from scratch. A new class can automatically obtain the existing properties and methods from the existing class through inheritance. This process is called inheritance; the existing class is called the parent class. , and the new class is called a subclass.

A subclass inherits all the properties and methods of its parent class, and can also define its own properties and methods. Inheritance is a rather figurative term, as if we inherit certain traits from our parents, such as dark hair, dark eyes, and yellow skin.

The definition of a subclass is slightly different from that of a normal class. Let’s look at it through an example. Suppose you want to define a class SpottedDog for Dalmatians, which is a subclass of the Dog class introduced in the previous section. The name of the subclass we define is SpottedDog, and then the parent class name Dog is added in parentheses after the class name, indicating that it is a new class inherited from the Dog class.

We define new properties and new methods for this new class, the code for the newly created subclass SpottedDog is highlighted below.

class Dog:   
    def __init__(self,name,gender):
    def Hello(self):
        print("My name is "+self.name+".")
        print("I am a "+self.gender+".")
        print("I want to play with you.")
class SpottedDog(Dog):
    def Character(self):
        print("I am a spotted dog.")
        if self.isLarge==True:
            print("I am a large dog.")

We define a new property isLarge and a new method Character() for the SpotedDog class. In the Character() method, check whether the isLarge property is equal to True, if the property is equal to True, “I am a large dog.” will be printed.

When we create an instance of the Dog class, the constructor will still remind us to enter the parameters name and gender, because the __init__() method of the parent class is inherited, as shown in Figure 1.

Inheritance of python classes
figure 1
We create an instance dog3 of the SpottedDog class, specify the dog’s name as “Lucky”, and it is a “Boy”. Then assign True to its isLarge property. Next, call the parent’s Hello() method and the child’s Character() method, as shown in Figure 2.

Inheritance of python classes
figure 2
We see that the subclass SpottedDog not only inherits the properties and methods of the Dog class, but also has its own properties and methods. We could also add the SpottedDog class’s own __init__() method, the modified code is highlighted below.

class SpottedDog(Dog):  
    def __init__(self,name,gender,spots):
    def Character(self):
        print("I am  a spotted dog.")
        if self.isLarge==True:
            print("I am a large dog")
        print("I have "+ str(self.spots) +" spots in my body.")

In the __init__() method of the SpottedDog class, we use a special function super(), whose function is to associate the parent class with the child class, so that the __init__() method of the parent class can be called. We also added a new attribute spots to this constructor, so when creating an instance of the SpottedDog class, we need to specify 3 parameters for the constructor, as shown in Figure 3.

Inheritance of python classes
image 3
Create a new instance dog4 for the SpottedDog class. Specify its three parameters in the construction method, which are “Happy” for the name, “Boy” for the gender, and “30” for the number of spots, as shown in Figure 4.

Inheritance of python classes
Figure 4
 When you call the Character() method again, you can see that the parameter passed to the constructor is already being used as the value of the spots property.

Leave a Comment