Does Python support multiple inheritance?

Object Oriented ProgrammingProgrammingPython

Yes, Python supports multiple inheritance. Like C++, a class can be derived from more than one base classes in Python. This is called Multiple Inheritance.

In multiple inheritance, the features of all the base classes are inherited into the derived class. Let us see the syntax −

Syntax

Class Base1:
	Body of the class

Class Base2:
	Body of the class

Class Base3:
	Body of the class
.
.
.
Class BaseN:
	Body of the class


Class Derived(Base1, Base2, Base3,  , BaseN):
	Body of the class

The Derived class inherits from both Base1, Base2 and Base3classes.

Example

In the below example, Bird class inherits the Animal class.

  • Animal is the parent class also known as super class or base class.

  • Bird is the child class also known as sub class or derived class.

The issubclass method ensures that Bird is a subclass of Animal class.

class Animal: def eat(self): print("It eats insects.") def sleep(self): print("It sleeps in the night.") class Bird(Animal): def fly(self): print("It flies in the sky.") def sing(self): print("It sings a song.") print(issubclass(Bird, Animal)) Koyal= Bird() print(isinstance(Koyal, Bird)) Koyal.eat() Koyal.sleep() Koyal.fly() Koyal.sing()

Output

True
It eats insects.
It sleeps in the night.
It flies in the sky.
It sings a song.
True
raja
Updated on 12-Aug-2022 12:19:02

Advertisements