×

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 Goto Statement

We all know that Python is the most basic and widely used programming language in the world. It is also one of the world's most popular and widely used languages....

4 minutes read.

Python program to print array element present at even position

Python program to print array element present at even position In this program, we'll see a Python program that prints the elements of an array that are in even positions. We...

1 minute read.

Face Recognition in Python

In this tutorial, we will understand what is face recognition and how it is achieved in python. Face recognition is of great utility in real-world scenarios. It is an extended step...

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

Scraping data in python

Data scraping is a technique in which one program extracts a set of data from the output of another program. Web scraping is the most common application of this technique....

6 minutes read.

Flutter with tensor flow in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The...

3 minutes read.

Python Dictionary copy() method

Python Dictionary copy() method The dictionary.copy () method in Python returns a copy of the specified dictionary. Syntax dictionary.copy () Parameter NA Return None Example 1 # Python program explaining # the dictionary.copy() method # initialising the dictionary ...

1 minute read.

Python bool()

Python bool() class The bool() class in Python returns the boolean value for the specified object. Syntax class bool([x]) Parameter object: This parameter represents the object, like String, List, Number etc. Return It will always return True, except...

1 minute read.

Anytree Python

Python's anytree module is frequently used within data science-related software. Anytree has a Tolerant License, a Construct File that is public, no errors, no risks, and excellent support. One may...

3 minutes read.

Accessing Key-value in Dictionary in Python

A Python word reference is an assortment of key-esteem matches where each key is related to worth. Worth in the key-esteem pair can be a number, a string, a rundown,...

5 minutes read.

Python if statement

Python if statement Decision making is an essential feature of any programming languages. Condition checking is the strength of decision making. Python provides many decision-making statements, such as: If statementIf-else statementelif statement if statement The if statement is...

2 minutes read.

Isodate Python

Python Programming Language 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...

3 minutes read.

Python pynmea2

Define Pynmea2 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...

4 minutes read.

Python TCP Server

The TCP server library is also known as the socket library. Socket programming is the method of connecting two nodes through a network to form communication between two nodes. In...

3 minutes read.

SKLearn Clustering

These are ml methods thatare responsible for detecting patterns and the similarities within the data.The clustering methods are unsupervised.Here the data is clustered to form groups with the help of...

3 minutes read.

Python Data Types

Python Data Types: The variable in Python is used to store values, and they can hold different values. Each variable in Python has its own data-type. Since Python is a...

13 minutes read.

Gethostbyname() function in Python

Python Programming Language 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...

6 minutes read.

Python Line Break

Introduction In this tutorial, you will learn line breaks in python.In Python, the new line character is used to indicate the start of a new line and the end of an...

5 minutes read.

Write Text files in Python

Let's discuss writing the text file in detail. Generally, writing text also follows the same procedure as like reading text. It varies only in some specific places. Steps followed for writing...

4 minutes read.

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered. Following...

3 minutes read.