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, when class A is inheriting from another class B, and class B has members with the same name as members in class A. 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 for super() function.




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


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

Using the object of a derived class to access these members of base class will only give access to its own members with the same name 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 its its member - instance variable, b
print('b = : ', ob_y.b)


# Using the object of class X to its 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
class X
a = :  10
b = :  20
class Y
i = :  20


Program Analysis





Question :


Can we access a member of the base class from within its derived class i.e. member which is having 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

PYTHON


Advertisement