previous | index | next

Polymorphism

A method is polymorphic if it has different behavior depending on the type of the object it is invoked on.

We can use polymorphism so that the following code:
    Item item = new OxfordShirtInfo(49.99, "Navy blue", 15, 33);
    item->display();
    item = new ChinoInfo(69.99, "Khaki", 36, 34, false);
    item->display();
produces:
  The shirt's price is 49.99
  The shirt's color is Navy blue
  The shirt's neck size is 15
  The shirt's sleeve length is 33
  The pants' price is 69.99
  The pants' color is Khaki
  The pants' waist size is 36
  The pants' inseam length is 34
  The pants are not cuffed.

previous | index | next