Python Multiple Inheritance
In our previous post, while we were talking about inheritance, a class
was inheriting the properties and functions of another class
. So what is multiple inheritance? Multiple inheritance is when a class
inherits the properties and functions of more than one class
.
Just like a child in real life has the characteristics of both his/her parents. Let's start coding right away, you will understand much better.
class Mother:
def print_Mother(self):
print("Features of the mother class")
class Father:
def print_Father(self):
print("Features of the father class")
class Child(Mother, Father):
pass
object_d = Child()
object_d.print_Mother()
object_d.print_Father()
Let's take a look at the output now;
Features of the mother class
Features of the father class
As you can see, we have taken and used functions of more than one class.
But there is a very important point that we need to pay attention to in multiple functions, we can only use a class
, a property or a function with the same name, let's give an example right away.
class Mother:
def eye_colour(self):
print("Features of the mother class")
class Father:
def eye_colour(self):
print("Features of the father class")
class Child(Mother, Father):
pass
object_d = Child()
object_d.eye_colour()
Both the Mother()
class and the Father()
class have the eye_colour()
property.
So, let's see which class the Child()
class will inherit from.
Features of the mother class
As we guessed, she took on the characteristics of the mother
class. Because first, the Mother()
class inherited its properties. Now let's change it and this time inherit the Father()
class first.
class Child(Father, Mother):
pass
first, we inherited the Father()
class, let's take a look at our output.
Features of the father class
Now you will say that why should I create a function with the same name, I will create a function with a different name. But remember, you may not be alone in a project. You may encounter such problems when you work with more than one person. Let's talk about what to do if the __init__()
constructor function exists in both classes. Let's show it now;
class Mother:
def __init__(self):
self.eye_colour = "Green"
print(self.eye_colour)
class Father:
def __init__(self):
self.eye_colour = "Brown"
print(self.eye_colour)
class Child(Father, Mother):
pass
object_d = Child()
As you can see when we run our code
Brown
We got an output like So, since we inherited the Father()
class first, the property of the Father()
class took precedence. Well, you will say now that we cannot change this order of priority ourselves when appropriate? Of course, let's show you how you can change it.
Constructor Function in Multiple Inheritance
class Child(Father, Mother):
def __init__(self):
Mother.__init__(self)
object_d = Child()
Let's look at the output;
Green
Here we have prioritized the __init__()
function of the Mother()
class. If you pay a little attention to the sentence, we only got the__init__()
function, not all the properties and functions of the Mother()
class. How can we get information from here? Let me explain right away that while we give priority to the __init__()
function of the Mother()
class, we can also give priority to another function of the Father()
class.
Let's show it with an example.
class Mother:
def __init__(self):
self.eye_colour = "Green"
print(self.eye_colour)
def height(self):
self.height = 1.72
print(self.height)
class Father:
def __init__(self):
self.eye_colour = "Brown"
print(self.eye_colour)
def height(self):
self.height = 1.87
print(self.height)
class Child(Father, Mother):
def __init__(self):
Mother.__init__(self)
object_d = Child()
object_d.height()
Output:
Green
1.87
As you can see, the child received the eye colour feature from the Mother and the height feature from the Father. The reason is, since we wrote the Father()
class first, it inherited the height()
function of the Father()
class. But since we specified the __init__()
function of the Mother()
class as priority, it inherited the __init__()
function of the Mother class as priority.
In the same way, if we gave priority to the height()
function of the Mother()
class, the height()
function of the Mother()
class would be taken.
class Mother:
def __init__(self):
self.eye_colour = "Green"
print(self.eye_colour)
def height(self):
self.height = 1.72
print(self.height)
class Father:
def __init__(self):
self.eye_colour = "Brown"
print(self.eye_colour)
def height(self):
self.height = 1.87
print(self.height)
class Child(Father, Mother):
def __init__(self):
Mother.__init__(self)
def height(self):
return Mother.height(self)
object_d = Child()
object_d.height()
When we look at the output, you will see that it inherits both properties from the Mother()
class.
Green
1.72
As you can see, although we wrote the Father()
class first, it took the properties of the Mother()
class because we gave priority to the properties of the Mother()
class.
Top comments (0)