ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.