×

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 and its advantages

What is Python? Python is an object-oriented programming language, a general-purpose programming language with a high level, and also an interpreted language that has an easy syntax and dynamic semantics. Python...

7 minutes read.

Number pattern in Python

Number pattern in Python: This article explains how to print number patterns in Python. The FOR loop, while loop, and range() functions are used in the following Python programs to...

4 minutes read.

Python program to sort the array element into ascending order

Python program to sort the array element into ascending order In this example, we'll see how to sort the array elements in ascending order using a Python program. Sorting is a...

2 minutes read.

Importing Numpy in Pycharm

Numpy : Numpy is one of the important library in python. The abbreviation of numpy is “Numerical python” or “Numeric Python”. This library is mainly used to access arrays. Numpy was created...

5 minutes read.

Python Breakpoint

Introduction In Python 3.7, a brand-new created function called breakpoint() was added. Due to the close relationship between both the executable and the code of a debugging component, debugging Python programming...

4 minutes read.

How to Install Pandas in Python?

Pandas is a library created in Python for handling and analyzing data. Pandas provides a variety of procedures and data structures for manipulating time series and numerical data.  Pandas is...

3 minutes read.

Handling missing keys in Python dictionaries

In this lesson, you will discover how to create a Python application that will manage missing keys in a dictionary. Python dictionaries store data as key-value pairs, where each value...

3 minutes read.

How to check data type in python

The data type represents the nature of a variable; it determines what kind of data it can store and what operations can be carried out on it. There are five...

4 minutes read.

Python String startswith() method

Python String startswith() method The string.startswith() method in Python returns a boolean value ‘True’ if the given string starts with the prefix, otherwise it returns False. Syntax startswith(prefix[, start[, end]]) Parameter prefix: This parameter signifies the value to check. start(optional):...

1 minute read.

Python Struct

Python 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 faster way....

3 minutes read.

Python Interpreter

In this tutorial, we will go through the basic knowledge about what an interpreter is and how we use it in the python programming language. It is one of the...

3 minutes read.

Python sorted() function

Python sorted() function The sorted() function returns a sorted list of the specified iterable object. Syntax sorted(iterable, *, key=None, reverse=False) Parameter iterable: It is a required parameter that represents the sequence to sort, list, dictionary, tuple etc. key:...

1 minute read.

How to Reverse a number in Python?

In Python, conditional statements can be combined with the list() method, slice() method, recursion() method, and other predefined techniques to generate reverse logical functions. The reverse() method is the most...

7 minutes read.

Google Chrome API in Python

Python: 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 faster way....

3 minutes read.

Python Percentage Sign

In Python, the percentage sign significantly completes two things. They are: It goes about as a Modulo administrator. It helps in string organizing. Allow us to see every one of them plainly. Modulo operator: Like...

2 minutes read.

Conditional Expressions in Python

In Python conditional expression are sometimes referred to operator called as ternary operator. Not only Python supports ternary operator but many other programming languages supports it. Ternary operator are the...

2 minutes read.

XGBoost for Regression in Python

Regression problem results real values. Decision Trees and Linear Regression are regularly used regression algorithms and use some metrics involved in regression like mean squared error and root mean squared...

5 minutes read.

Python Fit Transform

In machine learning, fit (), transform(), fit transform () methods are provided by scikit learn package. This package is used in model fitting and data processing. These methods are implemented...

2 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.

Import Function in Python

In Python programming language, the import keyword plays an important role in the whole code because it makes one module make available in another module. Import is used to structure...

3 minutes read.