Lecture 17 Inheritance II

We'll give more examples for inheritance in this tutorial. Please type these code in Spyder by yourself and test them out.

1. Why we need inheritance

Suppose we are creating a program which deals with various shapes. Each shape has some common properties. For example, the color of the shape, whether it is filled or not and so on. In addition to that, there are some properties which vary from shape to shape. For example, area and perimeter. The area of the rectangle is width * length whereas the area of the circle is πrē. At first, it might be tempting to create classes for different shapes like this:



Please try to not look at this code and type it out on your own after you understand this code.

Now we change the parameters of the object and print out the fill and the color again:



Now let's create a new Class called 'Circle'.



Did you notice that amount of duplicate code we are writing?

The marked area on the left-hand side is almost the same as the ones we defined in the Rectangle Class.

YES, that's why we need inheritance, AGAIN!

The structure to implement inheritance is just as simple as the following example:



See how I reused all the common attributes between my Child class and the Parent class.
I instantiated an instance from the child class 'Circle' which doesn't have the 'getColor()', 'setColor()', 'getFilled()', and 'setFilled()' function but I can still call these functions because of inheritance.



This made my code WAY shorter.

Please do type it out in Spyder on your own and play around with it.

2. Multiple inheritance.

Python allows us to derive a class from several classes at once, this is known as Multiple Inheritance. Its general format is:



For example:



3. Polymorphism method overriding

In literal sense, Polymorphism means the ability to take various forms. In Python, Polymorphism allows us to define methods in the child class with the same name as defined in their parent class.

As we know, a child class inherits all the methods from the parent class. However, you will encounter situations where the method inherited from the parent class doesn’t quite fit into the child class. In such cases, you will have to re-implement method in the child class. This process is known as Method Overriding.

If you have overridden a method in the child class, then the version of the method will be called based upon the type of the object used to call it. If a child class object is used to call an overridden method then the child class version of the method is called. On the other hand, if parent class object is used to call an overridden method, then the parent class version of the method is called.

The following program demonstrates method overriding in action:



Here b_obj is an object of class B (child class), as a result, class B version of the explore() method is called. However, the variable a_obj is an object of class A (parent class), as a result, class A version of the explore() method is called.

If for some reason you still want to access the overridden method of the parent class in the child class, you can call it using the super() function as follows:



Now let's work on the tasks to practice.


Tasks:

1. Imgine your are developing a part of an online shop:
Parent class:
Name: Products
Attributes: category, name, vendor, price
Methods: totalDollar()

Child class 1:
Name: Laptop
Attributes: category, name, vendor, price, discount

Methods: totalDollar()

Child class 2:
Name: Diaper
Attributes: category, name, vendor, price

Methods: totalDollar()

* Build these classes in Python, inherit all the common attributes from the parent class.
* The Laptop.totalDollar() method will apply the 20% off discount to the laptop and definitely update the subtotal balance (a global variable).
* The Diaper.totalDollar() method will ask the customer to provide the promotion code. If the promotion code matches 'CE232', then apply 50% discount to the diapers. Definitely update the subtotal balance (a global variable) after this.
* Should use the 'input()' function to let users provide the 'quantity' of the laptop needed, and the 'quantity and the promotion code' for the diapers.

To define a global variable 'subtotal', you can declare it in the first line of the code (the very top line), say 'subtotal = 0'. Then in every method, if you need to update it in a specific method, declare 'global subtotal' in side the function which will bring this global veriable to the local domain.

The user interface should look like this:



2. Add special methods __str__ to your code to enable this output:



The ones with an 'underscore' are variables. Zbook belongs to Laptop.name, HP belongs to Laptop.vendor. 'Durable' belongs to Diaper.name, 'Pampers' belongs to Diaper.vendor.