×

Python Super function

The super function is used to access and call the methods or functions involved in the parent class during Inheritance.

What is Inheritance?

The process where the parent class inherits some of its members, including methods, variables, etc., to the child class or subclass is known as Inheritance.

Condition for implementing super function:

The super function should contain the same arguments as the function that is called by the object.

Uses of super function:

  • We need not specify the methods and members present within the parent class while calling them from the child class / sub-class.
  • Supports code optimization and reusability as there is no need to rewrite the functions that are being called.
  • Working with Multiple Inheritance can proceed with the help of the super() function. So, it obeys Multiple Inheritance.

The super function can be implemented in:

  1. Single Inheritance
  2. Multiple Inheritance

Syntax / General usage of Super function:

super().__init__()

Single Inheritance using Super function:

If the child classes inherit members only from a single parent class, then that inheritance is called Single Inheritance. It consists of a Child class that inherits members from a Parent class. 

Super function in Python

We shall discuss the implementation of the super function in Single Inheritance with an example.

Example:

Let’s consider an example of vehicles. Cycles, Bikes, Cars, etc., come under the category of vehicles. They share a common point, i.e., we can ride all of them, and they make transportation easier.

So, we can conclude that these classes are the child classes of Vehicles (a parent class for all those classes). For example, let's frame a program using a super function and find the output.

Parent class: Vehicles

Child classes: Cycles, Bikes, Cars.

Multiple Inheritance using Super function:

If the child classes inherit members from more than one parent class, then that inheritance is called Multiple Inheritance. It consists of Child class which inherits members from two parents i.e., Father class and Mother class.

Super function in Python

We shall discuss the implementation of the super function in Multiple Inheritance with an example.

Example:

Let’s consider an example of Animals. Mammals, Non-winged Mammals, Non-marine Mammals, etc., also come under the category of Animas. They share a common point, i.e., they are Animals. Non-winged Mammals, Non-marine Mammals also have a common point, i.e., they belong to the mammals' category.

So, we can conclude that these classes are the child classes of Animals (which is a parent class for all those classes) and Non-winged Mammals, and Non-marine Mammals classes are the child classes of Mammals class. Again, being a mammal, “Non-winged Mammals” and “Non-marine animals” are a part of “Animals”. Now, let a class Dog be taken. It is a combination of Non-winged mammals and Non-marine mammals. So, the dog undergoes Multiple Inheritance.

Parent class: Animals.

Child class: Mammals.

Grandchild classes: Non-winged Mammals, Non-marine Mammals

Great grandchild class: Dog(Non-winged Mammals, Non-marine Mammals)

Here, the father class is the " Non-winged Mammals “ class, whereas the mother class is the " Non-marine Mammals “ class.

Let's consider an example program which describes a super function.

Program:

class vehicles:
    def __init__(self,w,p):
        self.wheels = w
        self.passenger = p
    def wheelNo(self):
        print("The number of wheels are ")
        print(self.wheels)
    def passengerNo(self):
        print("The maximum number of passengers are ")
        print(self.passenger)


class Bikes(Vehicles):
    def __init__(self,w,p):
        print("Bikes: ")
        super().__init__(w,p)






class Cycles(Vehicles):
    def __init__(self,w,p):
        print("Cycles: ")
        super().__init__(w,p)


class Cars(Vehicles):
    def __init__(self,w,p):
        print("Cars: ")
        super().__init__(w,p)




# objects being created in child classes
cy1 = Cycles(2,2)
cy1.wheelNo()
cy1.passengerNo()
b1 = Bikes(2,2)
b1.wheelNo()
b1.passengerNo()
ca1 = Cars(4,5)
ca1.wheelNo()
ca1.passengerNo()

Output:

Cycles: 
The number of wheels are 
2
The maximum number of passengers are 
2
Bikes: 
The number of wheels are 
2
The maximum number of passengers are 
2
Cars: 
The number of wheels are 
4
The maximum number of passengers are 
5

Explanation:

The classes " Bikes ", " Cycles " and " Cars " are inherited from the class " Vehicles ". So, " Vehicles " is the parent class whereas the classes Bike, Cycles and Cars are the child classes of Vehicle. The methods that define the number of wheels and passengers are declared in the parent class " Vehicles ". But, we have created objects in the child class. Although, we can access those methods when we use the super function. As the super function is used in all three child classes, the statements present in the parent class methods have been printed.


Related Topics

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal We know that the most widely used number system is a decimal system, but the computer only understands binary values. The...

2 minutes read.

Find Last Occurrence of Substring using Python

Introduction When planning to work with strings, we may need to determine whether a substring is present. This issue is rather typical, and there have been numerous discussions about how to...

3 minutes read.

Sort Dictionary in Python

Dictionaries In Python, a dictionary is an unordered collection or set of data types that enable of store data in an unordered key-value/pair. The key is stored alongside with value. Dictionary contains key:...

4 minutes read.

Python String Variable

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

4 minutes read.

Difference between python list and tuple

In this tutorial, we will understand the key differences between python lists and tuples in python. Firstly, let us see the similarities between Lists and Tuples. Both are two data types...

4 minutes read.

Best Way to Learn Python for Free

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. Learning Python is a step towards coding. Python gets...

5 minutes read.

What is Collaborative Filtering in ML, Python

Introduction Contents recommendation is a useful tactic for almost any specific technology looking to increase interest, but it frequently calls for a lot of user data and perhaps laborious content tagging...

3 minutes read.

Yield vs Return in Python

In this article, you will learn about " Yield " and " Return " in Python. You will also understand the difference between " Yield " and " Return "...

6 minutes read.

XXhash Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

6 minutes read.

Periodogram in Python

Python:  Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Tuple in Python

Tuples Tuples are the same as the list but as we know that lists are mutable means we can change the data from it. In the tuple we cannot change the...

6 minutes read.

Python MySQL Database Connection

In this article, you will be able to understand how to connect to a MySQL database through Python. We generally can use many methods or modules to connect to the database...

6 minutes read.

Sentence to python vector

Conversion of a Sentence to Vector in Python Before starting the tutorial, let’s just recap about the vector and the respective package that has to be imported in Python. Python Vector: Putting simply,...

3 minutes read.

Python try except

Before diving right into loads of syntax we need to know what does try except is used for and how it helps users in writing programs What is Python try except? Python...

5 minutes read.

Notepad++ For Python

In this article, you are going to learn what notepad++ is and how it is integrated with python to use the python programming language. You can also learn how to...

4 minutes read.

Python frozenset()

Python frozenset() class The frozenset() class in Python returns a new frozenset object, optionally with elements taken from iterable. Syntax class frozenset([iterable]) Parameter iterable: This parameter represents an iterable object, like list, set, tuple etc. Return This class returns an unchangeable...

1 minute read.

Python Array

Python Array In the programming language or computer science, an array is defined as the form of a data structure which consists of or store collection of various types of elements...

10 minutes read.

Nested Tuple in Python

If you are a Python learner, this page contains all the information that helps you know about nested tuple and how to access it in python. What is a Tuple? Multiple items...

4 minutes read.

Python TypeError

What is TypeError in python? TypeError is one of the exceptions in the python programming language. This exception occurs when an operation is performed on an unsupported object type or can...

6 minutes read.

Parameter Passing in Python

In Python, when we characterize capabilities with default values for specific boundaries, it is said to have its contentions set as a possibility for the client. Clients can either pass their...

3 minutes read.