7 Design subclasses right

 

This chapter covers

  • When to override and when to overload methods
  • Type hints and multimethod
  • The Liskov Substitution Principle
  • Designing classes and subclasses properly with is-a and has-a relationships
  • The Favor Composition over Inheritance Principle
  • Factory classes
  • Using Programming by Contract correctly when designing subclasses

As described in section 1.8, inheritance is one of the main concepts of object-oriented programming. When we create an application with class hierarchies of superclasses and subclasses, we must design the subclasses right to achieve a well-designed application.

7.1 When to override and when to overload methods

7.1.1 Override superclass methods to get subclass behavior

7.1.2 Type hints and method overriding

7.1.3 Overload methods that have similar or equivalent behaviors

7.1.4 Overloading with multimethod and type hints

7.2 The Liskov Substitution Principle and proper subclasses

7.3 Choosing the is-a and has-a relationships

7.3.1 Using is-a

7.3.2 Using is-a with multiple inheritance

7.3.3 Using has-a

7.4 Use a factory function with the Code to the Interface Principle

7.5 Programming by Contract with subclasses [optional]

7.6 Summary