Polymorphism

Identity and Behaviours

We have 3 different types of classes now. Our superclass is the Person class and we can think of the objects as the diagram below. 

So when we call the sayHello method of a Person object it calls its (original) version of the sayHello method. But the Player and Coach classes both inherit and then override their inherited copy of the sayHello methods with their own version.  This is a form of polymorphism.

So a Player object can use its own methods such as changeShirtNumber or switchCaptain (which are only available to the Player class) it can also use the getName, getAge and setAge which it inherited from the Person class. It also has it's own copy of the sayHello method which it has inherited and then over-ridden.

What is polymorphism?

There are many types of polymorphism and one of them is overriding. This is when a class redefines an inherited method.

So when we call the sayHello method of the Player class it is calling its own version of the sayHello method that it inherited from the Person class. 


This is why a Player object will give a different output from the SayHello method compared to the Coach class.