10) e. Multiple Inheritance Examples




In Python, defining a method in the child class with the same name and parameters (also known as method signature) as the method in the parent class results in method overriding.

In this case, if an object of the child class is used to invoke the method, then the version of the method implemented in the child class will be called. If an object of the parent class is used to invoke the method, then the version specified in the parent class will be called.

Observe the classes King and Prince below. The method find_empress is implemented in both classes. Run the code below and see it’s output. Focus on how we get different results even though we’re calling the same method.

1
2
3
4
5
Output:
I am the ruler's son
This is my princess
I am the ruler
This is my queen




Define a class Person and its two child classes: Male and Female.

Define a method getGender in the parent. The getGender method of the parent class Person will print “Unknown”.

Override the getGender method in both child classes so that it prints “Male” for Male class and “Female” for Female class.

1
2
3
4
Output:
Unknown
Male
Female




Although the following example looks lengthy, don’t worry it isn’t complex. It’s very simple if you focus on the inheritance concepts.

At the zoology lab, you are studying animals. The instructor wants you to observe the simple attributes of the creatures. We want to differentiate groups of Animals based on their feeding natures.

You are given the following parent class Animal.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#### Parent class ####
class Animal:
    # properties
    multicellular = True
   
    # eukaryotic means Cells with Nucleus
    eukaryotic = True

    # function breathe
    def breathe(self):
        print("I breathe oxygen.")

    # function feed
    def feed(self):
        print("I eat food.")

Now, create a child class named Herbivores. This class will extend Animal, and implement its own method feed. This method should print “I eat plants only. I am vegetarian”. Set an attribute for this class called strong_canine_teeth, assigned to False.

Also, create another child class named Carnivores. This class will also extend Animal, and implement its own method feed. This method should print out “I eat other animals. I am not vegetarian”. Set an attribute for this class called strong_canine_teeth, assigned to True.

Take a breather here. We’re almost done. Once you’ve created the child classes you would want to use them.

Finally, create two objects. One as an instance of Herbivores and the other as an instance of Carnivores. Using these two objects, print out its class properties (multicellular and eukaryotic attributes) from the parent class, and call parent method breathe() and child’s method feed(). Also, print out the child attribute strong_canine_teeth.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Output:
Is herbivore multicellular? True
Is herbivore eukaryotic? True
Is carnivore eukaryotic? True
Is carnivore eukaryotic? True

Herbivore child object:
I breathe oxygen.
I eat only plants. I am vegetarian.

Carnivore child object:
I breathe oxygen.
I eat other animals. I am not vegetarian.

Does herbivore have a strong canine? False
Does carnivore have a strong canine? True




Define a parent class named Shape and its subclass Square.

In the Square class define the constructor (__init__ method) which takes length as argument.

Define a function called area in both classes which returns the area as length * length.

1
Output:




Arlo has started to learn python. He is learning about classes and has been given an assignment by her professor to create a class simulating a student. The professor has given Arlo full independence in the number and names of attributes. Please create an empty Student class for Arlo. He will learn and fill up the details later.

1
Output:




Arlo has started to learn python. He is learning about classes and has been given an assignment by her professor to create a class simulating a student. The professor has given Arlo full independence in the number and names of attributes. Please create an empty Student class for Arlo. He will learn and fill up the details later.

1
Output:




Arlo has started to learn python. He is learning about classes and has been given an assignment by her professor to create a class simulating a student. The professor has given Arlo full independence in the number and names of attributes. Please create an empty Student class for Arlo. He will learn and fill up the details later.

1
Output: