How Do I Call A Parent Class’s Method From A Child Class In Python?

To learn more about How to call a parent class’s method from a child class in Python we do use a keyword that is super for getting the methods of the parent class in python.

Parent

Parent Class

In Objective oriented programming languages we have inheritance where we can directly inherit the properties of the parent class into the child class and from the child class when we have to call the function of the parent class then we have to use the super keyword to call it.

As the super keyword is used for referring to the members of the parent class where as this keyword is used for referring to the same class functions or variables in python.

This keyword is really very useful when it comes to inheritance as it prevents us to write the same line of code multiple times and directly using it simply by inheriting the properties and in properties, it comes all the methods, Variables, and as a whole class.

To understand the concept of super and inheritance and the use of parent class members in child class follow the example given below and learn.

class Animal():
    # This is constructor for python
    def __init__( self, name, age):
        self.name = name

    def print_attribute( self): 
        print("I have legs!")

class Dog(Animal):          # Name of the parent class must be included within the brackets
    # This is constructor for python
    def __init__( self, name, age):
        super().__ init__(name, age)     # Notice how we do not need self parameter here

my_ dog = Dog(" Roxas", 26) 
my_ dog. print_ attribute()

 

 

To learn more about Inheritance and the use of super keywords visit: Use of Super keywords for accessing the functionality of the parent class.

And t learn more about programming languages visit Python’s list: What is the difference Between methods Append and Extend?

And to learn more about different programming languages, and solve the problems related to it visit. beta python programming languages Solutions.

Leave a Comment

%d bloggers like this: