Q:

Python program to illustrate constructor inheritance

belongs to collection: Python class & object programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python class & object programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to check prime number using object ... >>
<< Example of Hierarchical Inheritance in Python (2)...