Programming Search⌘K

Dealing with inheritance

The refactorings of chapter 12 that move features up and down a class hierarchy or replace inheritance with delegation.

Techniques

  • Collapse hierarchy

    Merge a subclass into its superclass, or the superclass into the subclass, when the two are no longer different enough to be two classes.

    core
  • Extract superclass

    Make a new superclass for two classes that have features in common, and move the shared fields and methods into it.

    core
  • Pull up constructor body

    Move the statements that the constructors of subclasses share into the constructor of the superclass, and call it with super.

    core
  • Pull up field

    Move a field that two or more subclasses declare and use in the same way into their common superclass.

    core
  • Pull up method

    Move a method that two or more subclasses define in the same way into their common superclass, and remove the copies.

    core
  • Push down field

    Move a field from a superclass into the one subclass, or the few subclasses, that use it.

    core
  • Push down method

    Move a method from a superclass into the one subclass, or the few subclasses, that use it.

    core
  • Remove subclass

    Replace subclasses that differ only in constant data with a field in the superclass, and remove the subclasses.

    core
  • Replace subclass with delegate

    Replace a subclass that holds one variation of behavior with a separate delegate object that the main class holds and calls.

    core
  • Replace superclass with delegate

    Replace inheritance from a class whose interface does not fit the subclass with a field that holds an instance of that class, and forward only the calls that are needed.

    core
  • Replace type code with subclasses

    Replace a field that holds a type code, such as "engineer" or "salesman", with one subclass for each value of the code.

    core