Object Oriented Programming using C#

(backadmin) #1

Object Oriented Programming using C#
Object Roles and the Importance of Polymorphism


Activity 5

Adapt the following diagram by adding a class for Diesel cars in such a way that it can be used to illustrate polymorphism.

Feedback 5

This is one solution to this exercise... there are of course others.

Here Mechanic is directly interacting with Car. In doing so it can interact with any subtype of Car e.g. Petrol, Diesel or any
other type of Car developed in the future e.g. (Electric). These are all different (different shapes – at least different internally)
and yet Mechanic can still interact with them as they are all Cars. This is polymorphic.

If an ElectricCar class was added Mechanic would still be able to work with them without making any changes to the
Mechanic class.
Free download pdf