In the previous lesson we discussed the abstraction in object-oriented programming Now, let's discuss another important concept in object-oriented programming inheritance A child has hereditary features like his/her parents For example, Crayon Shin-chan has a special eyebrow which is from inheritance In the concept of object-oriented programming based on an abstraction, conduct detailing and modifications detailing and modifications to generate a new abstraction which is, in object-oriented programming, called "inheritance" For example the parent class is vehicles having four wheels independent power, able to run Based on that, we may derive cars, ambulances, buses, etc Here, a car details the vehicle size restricting the passenger numbers Also, a bus or an ambulance gives specific definitions of uses which is known as inheritance Inheritance is a very important factor for the powerfulness of object-oriented programming Inheritance is to use a well-defined class Through inheritance we extend or modify it without the need to realize the original class again In fact, it is a relatively "lazy" way Sure, this "lazy" is a commendatory term When deriving subclasses we need to pay attention to some design principles Generally a subclass is a more specific and detailed type of parent class with a relation of "is-a" namely, "one of it" For example, a car is a vehicle an ambulance or bus is also a vehicle Under such circumstances, we would use the method of derivation But, we also see some improper methods One example is to take date as the parent class to derive a class representing a historical transaction statement After further learning about the cause of such a method we know that the developer only considered the date information included in the transaction data and, for repetitively using the dates historical transaction statements are derived from them Let's return to our previous topic We just mentioned that a subclass is a more specific and detailed type of a parent class with a relation of "is-a" Then, how about historical transactions and dates What's the relation between them Is there only a relation of "one of it" namely, "has-a" We should pay attention to that during designing Let's go back to the GUI class library in Python again In the GUI class library in Python there are also many typical examples such as the Control parent class which has such capacities as creating, displaying/hiding and setting labels and responding to user input events As for Button, compared to Control it has, in addition to all those capabilities of Control methods similar to SetDefault() which is able to set a given Button as a default button BitmapButton is a subclass of Button which has in addition to those attributes and methods of Button the ability to provide a creating method of specifying bitmaps and a method of modifying the bitmap on button upon creation We can view all their relations on the official website of wxPython Let's have a look On the official website of wxPython, we can view such relations of inheritance among classes Well, how can we define a subclass The syntax for defining a subclass seems no difference from that for an ordinary class a "class", followed by the class name then by the parent class from which you need to derive from If your class has no ancestor at all for derivation you might use "object" as the name of parent class As we mentioned before "object" is the root of the class hierarchy If there is only one parent class we call it a single inheritance for multiple parent classes, it is called a multiple inheritance Not all languages support multiple inheritance Java, for example, does not support inheritance of multiple parent classes while Python supports multiple inheritance, like C++ A multiple inheritance has its own upsides but, in fact, also brings many troubles So, we should be clear about it before using it Let's look at an example It's still about puppies Here, we defined a subclass BarkingDog whose parent class is Dog BarkingDog can inherit all the methods and attributes of Dog Sure, inheritance is only the first step As mentioned before, when a subclass is derived the method of parent class may be overrided Almost all methods including operators and initiation methods, can be overrided In this example, as we see the subclass overrides the greet() method of parent class with changes made to print and statement It's worth mentioning that if the subclass overrides the initiation method of parent class the initiation method of parent class would then not be automatically called it will be executed only when it is explicitly written so A common method is to use "super" to refer to the parent class of current object Here, if we're to override the __init__() method of the parent class “Dog” the first statement in the __init__() method of subclass may just be written as super().__init__(name) That's the defining and overriding of subclass You might imitate it and have a try by yourselves After learning the definition of class and the basic concepts of inheritance and rewriting let's look at a simple comprehensive example: BMI calculation BMI, which I guess you guys all know is a common criteria for obesity and healthiness Its calculation formula is body weight divided by height squared Look at the requirement First, look at Question (1) As required, first, define the __init__() method Calculate the BMI index and then define the printBMI() method Output the BMI index and then use the specific height and weight data in the instance This is Question (1) Question (2) requires to, according to the reference BMI criteria for China override the printBMI() method output more detailed classifications and information on morbidity risks and then use the specific height and weight data in the instance Let's briefly look at several critical values in the BMI criteria for China The first one is 18.5, underweight low morbidity risk As they are rounded off to the first decimal place the several critical values are 24, 27, and 30, respectively Let's write this program First, define the BMI class Define the __init__() method The first argument is "self" As it's needed to calculate the BMI value with the __init__() method in the instance we need two arguments: "height", and "weight" Then, let's define the printBMI() method Round off to the first decimal place BMI has been well defined Next, let's apply it to an instance First, input the height and the weight The method of class is bound to the instance in this way Then, use the instance x to call the printBMI() method Run this program For example, the height is 1.6, and the weight is 55 BMI will be 21.5 Question (1) is finished Proceed to Question (2) We must override the printBMI() method Define the ChinaBMI class Its parent class is BMI Let's override the printBMI() method First, we output the BMI value and then judge and output the BMI classifications and morbidity risks Output the information of BMI value in Chinese Round off to the first decimal place and then discuss the BMI classifications When BMI is smaller than 18.5 output information like underweight and low morbidity risks elif When the BMI value is smaller than 24, output the corresponding information and then judge and output all the cases in this way Moreover, at the location of instance, just change the class name BMI into ChinaBMI Still call the printBMI() method The only difference is that it calls the printBMI() method in the subclass ChinaBMI We have roughly finished writing this example I'm convinced that through such a simple example you must've had more thorough understanding of how to define methods in classes how to apply them to instances and how to override methods in classes