Python super() Function
As we all know that, Python is an object-oriented programming language. Therefore, Python follows all the concepts of OOPs, and one of such concepts is inheritance.
While using the inheritance concept, we can refer to a parent class with the use of super() function inside the inherited or child class. The super() function we use in the child class returns a temporary created object of the superclass, that allow us to access all of its method present in the child class.
Benefits of super() function:
Following are the benefits of using a super() function in child class:
Constraints of using super() function:
Following are three constraints that we must have to follow to use the super() function in a Python program:
Now, as we know that, we can use the super() function in both types of inheritances in Python, i.e., single as well as multiple inheritances. Therefore, we will learn about using the super() function in both types of inheritance separately and with an example.
Using super() function in single inheritance in Python
In this example, we will use animals as the reference for a single inheritance example.
Cats, horses, cows, dogs, etc., all are part of class animalia. They all share some common characteristics also:
So, we can say that the class cats, class horses, and class dogs are the subclasses of the class animalia. This is an example of single inheritance because all the subclasses (class cats, class horses, and class dogs) are inherited from a single parent class only, i.e., class animalia. Now, look at the following program.
The given animal is a mammal type. The given animal is a domestic animal type. The given animal has four legs and a tail.
In the above code, we have defined animalia as parent class and inherited domestic, tail & legs, and mammal class from it. After that, we have defined the cat, horse, and dog class and used super function in it. With the help of super() function in these classes, we have accessed methods of animalia class in cat, horse & dog class.
Using super() function in multiple inheritances in Python
In this example, we will use a parent class, i.e., mammal class. Then, we will inherit the 'Can Fly' and 'Can Swim' classes from the mammal class.
These classes will represent if a given mammal can fly or not and can swim or not. We will define an animal class after that, and it will inherit from both 'Can Fly' and 'Can Swim' class and return us that given animal have the defined characteristics or not.
So, we can see that the animal class we are using here is inherited from multiple base classes, and therefore, it is an example of multiple inheritances in Python. Now, look at the following program.
Cat is not capable of flying Cat is not capable of swimming Cat Is a mammal of animalia class
In the above code, we defined mammal as a parent class. After that, we inherited can fly & can swim classes from the mammal class. We used the methods of both can fly & can swim inside the animalia class with the help of super() function. The animalia class is inherited from both the can swim and can fly class.
Next Topicmax() function in Python
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week