Welcome to this tenth Python tutorial where we will do the second part of object-oriented programming, but this time we will explain what is inheritance and multiple inheritance, remember that it will be understood that you read the previous tutorial, first we will proceed to explain the inheritance with a very practical exercise followed by multiple inheritance, let's begin.
Before including your term within the programming, you can define the inheritance as attributes that are passed from generation to generation either between animals or things, for example, you can inherit a mole, attitudes, skin color among many things more than your father, mother, grandfather, etc., in object-oriented programming we can make use of this term, since objects can inherit functions, attributes, methods or properties of another object, this in code would be called "class inheritance" where there is a main class called "super class" and a derived class known as "sub class".
To see how this would be within the object-oriented programming we will use a very famous series called "Game of Thronos" we will use the Stark family, we will create a super class, sub class with their respective attributes, let's see the following image:
Through this family we will do the above mentioned, taking into account the inheritance of the children of the Stark parents which will be the objects in our code, always use the previous reference image so that you can clearly understand the concept of the code that will be given then.
First we will create our Super class, which will be represented by the pillars of the Stark family which are Eddard and Catelyn, then we will make the sub class where will be inherited attributes of the super class, this is where the objects that in this case are the children of the Stark pillars will have different names, but they will share the same surname, let's see the following code:
class FamilyStark:
"""Super class family Stark"""
print ("Children of Eddard and Catelyn Stark")
def __init__(self, name_stark):
self.name_stark = name_stark
class InheritorStark(FamilyStark):
"""Sub class that inherits attributes
of the FamilyStark class"""
def name(self, name, name_stark):
print("My name is", name, "heir of the house", name_stark)
In the Super class we only define the surname "Stark" which will be the inherited attribute for all the objects in this case, in the sub class "InheritorStark" we call the super class, here we only take into account the name of each object and we will take the inheritance that would be the surname "Stark", now we will proceed to instantiate the objects and finally access the attributes and methods adding the following lines of code:
#We instantiate the objects of the sub class InheritorStark
robb = InheritorStark("Stark")
sansa = InheritorStark("Stark")
arya = InheritorStark("Stark")
bran = InheritorStark("Stark")
rickon = InheritorStark("Stark")
#We access the methods and attributes
print(robb.name("Robb", robb.name_stark))
print(sansa.name("Sansa", sansa.name_stark))
print(arya.name("Arya", arya.name_stark))
print(bran.name("Bran", bran.name_stark))
print(rickon.name("rickon", rickon.name_stark))
As we can see in the case of the object "robb" we assign the surname "Stark" to the moment we use the sub class "InheritorStark ()", the value that we place inside the parentheses will be the inheritance that we define in the Super class in line 5 of the previous image, finally let's get to the methods and attributes taking the parameter ".name" we give the name to the object, only the inheritance is missing, we use "robb.name_stark", that way we will obtain the following result:
If they are all set to inherit the surname "Stark" with totally different names, the "none" value that shows on the screen is due to the fact that we are not returning any specific value in the sub-class method, at the beginning it can be a bit confusing , what must be taken into account is the attribute that we want to inherit all the objects, this helps us to classify the objects, if we take into account the other families each one would have its own Super class and sub class.
Python is a language that allows a sub class to inherit attributes of different classes, this is done when we know that an object has different common attributes of classes already created, for that we will take a simple example of a family, where there is a grandfather, mother and a son, where each one would be a class, taking into account that the grandfather would be the super class and the other sub classes, let's see the following code:
class Grandfather: #super class
def __init__(self, height):
self.height = height
class Father:#Sub Class
def hair(self):
return "Black"
#class that inherits attributes of grandfather and father
class son(Grandfather, Father ):
pass #is used when no action is necessary
As you can see we created the three previously indicated classes, in this case we are going to instantiate the object with the name of "Pedro" which inherited from his grandfather the height, but from his father the hair of color "Black", we simply added these lines of codes to instantiate and access the methods:
pedro = son("tall")
print("Thanks to my grandfather I am", pedro.height)
print("For my dad my hair is", pedro.hair())
ext = input("Press enter to exit")
In this way the object "pedro" is taking attributes of two classes this we can do with infinities of classes, but for practical reasons we only take two, but if you like you can add a class called "mother" and make an attribute for our object " pedro ", the final result of this small exercise would be the following:
So we conclude with the essential part of object-oriented programming in Python, it is up to you to practice with different exercises that you can perform yourself taking as examples the aforementioned, likewise in the next tutorials we will use everything we have learned to carry out programs with all the terms and codes that we have seen. Remember that if you have any questions you can use the comment box. See you at the next one!