Friday, December 27, 2024
Google search engine
HomeLanguagesPython: Call Parent class method

Python: Call Parent class method

A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made.

Example:




# Python program to demonstrate
# classes
  
  
class cls:
  
    # Constructor
    def __init__(self, fname, mname, lname):
        self.firstname = fname
        self.middlename = mname
        self.lastname = lname
          
    # class Function
    def print(self):
        print(self.firstname, self.middlename, self.lastname)
  
# Use the Parent class to create an object
# and then execute the print method:
x = cls("Geeks", "for", "Geeks")
x.print()


Output:

Geeks for Geeks

Calling Parent class Method

To understand about the concept of parent class, you have to know about Inheritance in Python. In simpler terms, inheritance is the concept by which one class (commonly known as child class or sub class) inherits the properties from another class (commonly known as Parent class or super class).

But have you ever wondered about calling the functions defined inside the parent class with the help of child class? Well this can done using Python. You just have to create an object of the child class and call the function of the parent class using dot(.) operator.

Example:




# Python code to demonstrate 
# parent call method 
  
class Parent:
  
    # create a parent class method
    def show(self):
        print("Inside Parent class")
  
# create a child class
class Child(Parent):
      
    # Create a child class method
    def display(self):
        print("Inside Child class")
  
# Driver's code
obj = Child()
obj.display()
  
# Calling Parent class
obj.show()


Output

Inside Child class
Inside Parent class

Calling Parent class method after method overriding

Method overriding is an ability of any object-oriented programming language that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.

Parent class methods can also be called within the overridden methods. This can generally be achieved by two ways.

  • Using Classname: Parent’s class methods can be called by using the Parent classname.method inside the overridden method.

    Example:




    # Python program to demonstrate
    # calling the parent's class method
    # inside the overridden method
      
      
    class Parent():
          
        def show(self):
            print("Inside Parent")
              
    class Child(Parent):
          
        def show(self):
              
            # Calling the parent's class
            # method
            Parent.show(self)
            print("Inside Child")
              
    # Driver's code
    obj = Child()
    obj.show()

    
    

    Output:

    Inside Parent
    Inside Child
    
  • Using Super(): Python super() function provides us the facility to refer to the parent class explicitly. It is basically useful where we have to call superclass functions. It returns the proxy object that allows us to refer parent class by ‘super’.

    Example 1:




    # Python program to demonstrate
    # calling the parent's class method
    # inside the overridden method using
    # super()
      
      
    class Parent():
          
        def show(self):
            print("Inside Parent")
              
    class Child(Parent):
          
        def show(self):
              
            # Calling the parent's class
            # method
            super().show()
            print("Inside Child")
              
    # Driver's code
    obj = Child()
    obj.show()

    
    

    Output:

    Inside Parent
    Inside Child
    

    Example 2:




    # Program to define the use of super() 
    # function in multiple inheritance 
    class GFG1: 
        def __init__(self): 
            print('HEY !!!!!! GfG I am initialised(Class GEG1)'
        
        def sub_GFG(self, b): 
            print('Printing from class GFG1:', b) 
        
    # class GFG2 inherits the GFG1 
    class GFG2(GFG1): 
        def __init__(self): 
            print('HEY !!!!!! GfG I am initialised(Class GEG2)'
            super().__init__() 
        
        def sub_GFG(self, b): 
            print('Printing from class GFG2:', b) 
            super().sub_GFG(b + 1
        
    # class GFG3 inherits the GFG1 ang GFG2 both 
    class GFG3(GFG2): 
        def __init__(self): 
            print('HEY !!!!!! GfG I am initialised(Class GEG3)'
            super().__init__() 
        
        def sub_GFG(self, b): 
            print('Printing from class GFG3:', b) 
            super().sub_GFG(b + 1
        
        
    # main function 
    if __name__ == '__main__'
        
        # created the object gfg 
        gfg = GFG3() 
        
        # calling the function sub_GFG3() from class GHG3 
        # which inherits both GFG1 and GFG2 classes 
        gfg.sub_GFG(10)

    
    

    Output:

    HEY !!!!!! GfG I am initialised(Class GEG3)
    HEY !!!!!! GfG I am initialised(Class GEG2)
    HEY !!!!!! GfG I am initialised(Class GEG1)
    Printing from class GFG3: 10
    Printing from class GFG2: 11
    Printing from class GFG1: 12
    
RELATED ARTICLES

Most Popular

Recent Comments