Python program to illustrate constructor inheritance

Here, we are going to illustrate constructor inheritance in Python.
Submitted by Shivang Yadav, on March 12, 2021

Here, we will see a Python to illustrate the working of constructor call using super() to call inherited class.

  • Constructor are the functions of a class that are invoked at the time of object creation.
  • Inheritance is the property of object-oriented programming in which one class inherits the properties of another class.
  • Inherited Class is the class whose properties are inherited by another class.
  • super() method is used to call the member of the inherited class in the current class.

Program to illustrate the working of our solution

class Animal:
    def __init__(self,x):
        print(x[0],"is Warm Blooded Animal")
class Marine(Animal):
    def __init__(self,x):
        print(x[0],x[1],"Swim")
        super().__init__(x)
class Wings(Animal):
    def __init__(self,x):
        print(x[0],x[1],"Fly")
        super().__init__(x)
class Dog(Marine,Wings):
    def __init__(self,x):
        print(x[0])
        super().__init__(x)
class Penguin(Marine,Wings):
    def __init__(self,x):
        print(x[0])
        super().__init__(x)

D=Dog(["Labrador","Can't"])
P=Penguin(["Penguin","Can"])

Output:

Labrador
Labrador Can't Swim
Labrador Can't Fly
Labrador is Warm Blooded Animal
Penguin
Penguin Can Swim
Penguin Can Fly
Penguin is Warm Blooded Animal

Explanation:

In the above code, we have a created 5 class that inherits in hybrid inheritance form depicted here,

constructor inheritance

There are two hybrid inheritances. Then we have created objects of class penguin and dog. Their constructors made calls to the constructors of their parent classes using super() method and printed the output.

Python class & object programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.