Making Classes Act the Same: Polymorphism
So far, I have shown how to take my little code library for products and turn it into a class, and shown how to encapsulate the properties and operations on a product. However, there is one aspect of my product class that still seems unfortunate: In the implementation of many of the product class's member functions, it still has to perform a comparison check of where the product comes from and then execute separate code paths. Worse, if I want to add new product sources (for example, if I were to team up with a boat manufacturer to start selling boats), I would need to dig into the code and add more cases and code paths to handle the new locations.
Ideally, I would like some way to create a series of classes that have the same public interface (defined as the publicly accessible variables and methods on a class) but have separate implementations that know about the various locations from which products come.