2.2.3 Polymorphism 

Previous Index Next


Polymorphism is the ability of an object to be treated as if it was one of it's ancestors.

For instance in the previous section we had three objects Account, SavingsAccount and CheckingAccount. Both SavingsAccount and CheckingAccount inherited from Account.

A developer could now write some code that took a group Account objects and calculated the average balance for the group of accounts. If the group of account objects is now set up to contain SavingsAccount and CheckingAccount object the code will still work because the SavingsAccount and CheckingAccount objects can be treated as their ancestor Account.

In fact if 6 months later a new kind of object ForeignAccount was created, inheriting from Account, the same piece of code would work without changes even if the group now included ForeignAccount objects.


1