python tutorial - Python Multiple Inheritance | Multiple Inheritance in Python - learn python - python programming




 multiple inheritance
  • 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.
  • The syntax for multiple inheritance is similar to single inheritance.
  • Python supports multiple inheritance also. You can derive a child class from more than one base (parent) class.

Python Multiple Inheritance Example:

class Base1:
    pass

class Base2:
    pass

class MultiDerived(Base1, Base2):
    pass
  • Here, MultiDerived is derived from classes Base1 and Base2.

Image representation:

 python multiderived class inherits
  • The multiderived class inherits the properties of both class base1 and base2.

Syntax of multiple inheritance:

class DerivedClassName(Base1, Base2, Base3):  
    <statement-1>  
        .  
        .  
        .  
    <statement-N>   
        Or
    class Base1:  
    pass  
  
    class Base2:  
    pass  
  
    class MultiDerived(Base1, Base2):  
    pass 

Example 1: multiple inheritance

class First(object):  
  def __init__(self):  
    super(First, self).__init__()  
    print("first")  
  
class Second(object):  
  def __init__(self):  
    super(Second, self).__init__()  
    print("second")  
  
class Third(Second, First):  
  def __init__(self):  
    super(Third, self).__init__()  
    print("third")  
  
Third();

Output:

first  
second  
third

Related Searches to Multiple Inheritance in Python

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×