Chapter 10. Polymorphism

By the end of the previous chapter, we had created a DatedStockItem class by inheritance from the StockItem class, adding an expiration date field. This was a solution to the problem of creating a new class based on the existing StockItem class without rewriting all of the already functioning code in that class. Unfortunately, however, it didn't allow us to mix objects of the original StockItem class in the same Vec with those of the new DatedStockItem class and still have the correct Reorder function called for the derived class object. So far in this book, we've seen how and why we use the first two major organizing principles of object-oriented programming: encapsulation and inheritance. Now our requirement of mixing base and derived class objects leads us to the third and final major organizing principle: polymorphism. Once we have defined some terms, we'll get right to using polymorphism to solve our problem of freely interchanging StockItems and DatedStockItems in our application programs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset