Advertisement



< Prev
Next >



Python - super() Function





In our previous article of Python, we have explained the concept the inheritance and its various types with examples. In this article, we are going to explain the concept of super() function in inheritance.

But before that, we need to understand a scenario in inheritance, where a class A is inheriting from another class B, and class A has members with the same name as members in class B. In this case, class B can be referred to as a base class/superclass, whereas class A can be referred to as a subclass/derived class.

Let us see this scenario through an example, which will lead us to the need of the super() function.




Inherited members of a base class redefined in its derived class.


A class inheriting from another class class can have members with the same name as the members in its base class, in this scenario, the base class members having the same name will be redefined in the derived class.

In this case, trying to access the members of a base class by using an object of its derived class will only give access to the members of derived class and not the members of its base class. Let us understand this concept through an example.

# Python - Members of base class redefined in the derived class.


# Base class
class X:
    a = 10
    b = 20.6

    # Method of class X
    def message(self):
        print('class X')

	
	
# Derived class Y, inherited from class X
class Y(X):
    b = 20
    i = 20

    
    # Method of class Y
    def message(self):
        print('class Y')


		

# Creating an object of class X
ob_x = X()


# Using the object of class X to access its member - instance variable, a
print('a = : ', ob_x.a)


# Using the object of class X to access its member - instance variable, b
print('b = : ', ob_x.b)


# Using the object of class X to access its member, method - message()
ob_x.message()




# Creating an object of class Y
ob_y = Y()


# Using the object of class Y to access the inherited member, a, of class X
print('a = : ', ob_y.a)


# Using the object of class Y to access the inherited member, b of class X,
# which is now redefined in class Z with the same name.
print('b = : ', ob_y.b)


# Using the object of class Y to call the inherited method, message() of class X
# which is now redefined in class Y with the same name, it will call the definition of message() in class Y
ob_y.message()


# Using the object of class Y to access its own member, i.
print('i = : ', ob_y.i)

Output


a = :  10
b = :  20.6
class X
a = :  10
b = :  20
class Y
i = :  20


Program Analysis





Question :


Can we access a member of base class from its derived class i.e. a base class member with the same name as a member of its derived class? Yes, it can be done by using the super() function.


Advertisement




Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement