Inheritance and Polymorphism

Our UML model so far

So far we have a single Person Class, shown in the UML class diagram to the right. 

We are going to expand this program by including Players and Coaches.

A player will have a shirt number and could be the team captain. And a coach will have a coaching qualification. 

We will look at how we can incorporate these two classes as efficiently as we can.

The UML diagrams for the Player and Coach classes are shown below

A player has a shirt number and might play as captain

Coaches can update their professional qualification

A lot of duplicated code…

If we look at our implemented classes you will see that there is a lot of duplicated code. 

We have duplicated code for the properties and some of the methods that are present in both classes.

Expanding our UML Diagram 

What does the colour coding mean?

The properties and methods in red are specific to that class

e.g. shirtNumber, changeQualification

The properties and methods in purple are common across different classes

e.g. firstname, getAge

The properties and methods in green have a similar name but perform a different purpose.

e.g. sayHello

Inheritance

We have now adapted the UML diagram from the left to the UML diagram on the right. This diagram begins to demonstrate the concept of inheritance. This is when a class can inherit copies of the properties and methods of a superclass. This is similar to how you inherit some characteristics from your parents.

inheritance relationship between classes (hollow arrow)


Player is a subclass of person, Coach is a subclass of person


A subclass inherits members from the superclass


Inherited members retain name, type and access


Subclass methods can override superclass methods


Subclass can include additional specific members

A superclass is more generalised than a subclass. 

A subclass is a more specialised class that will apply to more specific objects. It will usually contain additional methods and properties that do not apply to objects that are instances of the superclass.

Our subclasses in Python

In the class name we have addded the parameter (Person) beside the class name. That indicates that the Player class will inherit the methods and properties of the superclass Person.  This means that Player is a subclass of Person.

In the constructor method of both the Player and Coach class we have added a call to the constructor method of the superclass super().__init__(firstname,surname)

This calls the constructor method of the super class (Person) and passes in the values for its properties firstname and surname which creates an object which is an instance of a Person class.

You will notice that the SayHello method is present in both the Player and Person classes. It performs a different operation in each of the two subclasses and will output an output message that is formatted differently. This is a form of polymorphism and we will cover more on this later.

Implemented Subclasses in Python

References

A variable is a reference to an object so when we run the following code

We would see the following in a debugger

So if we now execute the following code

We would see the following output

When we executed the code newPlayer2 = newPlayer1 we changed the reference for the object newPlayer2 to point to the newPlayer1 Object.

This matches up with the information we would see from a debugger.

Before we execute the code newPlayer2 = newPlayer1

Before

You will see that the two variables are pointing to different memory addresses

After

You will see that the two variables are now pointing to the same memory address ( the same object)