Introduction: The rest of OOP 😎

And now the course begins to wind down...

Last week's Wumpus program is the largest one we'll work on this term at around 400 lines of code. You've come a long way from the early weeks of the course when getting a dozen lines right could be challenging.

This week we'll finish off the remaining two parts of OOP: inheritance and polymorphism. Inheritance is a fairly straightforward idea and is achieved with just one small syntactic addition. Adding inheritance to large programs often means getting to delete lots of code, so programs become shorter and clearer: win-win! To explore inheritance we'll revisit playing cards.

Polymorphism isn't difficult to understand, but implementing it often involves adding lots of code. Fortunately that code is largely boilerplate, as opposed to subtle algorithms, and the benefit is that we can make our types look like built-in types by using operators like + and * to combine them. Our example for polymorphism is fairly pedestrian, fractions, so we can keep it simple and focus on the new syntactic elements polymorphism uses.

  1. Inheritance and Polymorphism
  2. Inheritance example: Playing Cards
  3. class CardCollection
  4. class Deck
  5. class Hand
  6. Putting the pieces together
  7. Aside: Refactoring
  8. Polymorphism
  9. Example: Fractions
  10. Reverse engineering
  11. class Fraction
  12. Example: Playing cards
  13. A subtle point: Semantics
  14. Conclusion: Adding Types