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.

Example

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()

In the following 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.

Output

True
True
It eats insects.
It sleeps in the night.
It flies in the sky.
It sings a song.
raja
Sri
Published on 25-Jun-2019 18:21:58
Advertisements