Pull Up Field

Two subclasses have the same field.

“Two subclasses have the same field.”

Move it to the superclass.

graph LR
    A[Subclass A::field] -->|Refactoring| B[Superclass::field]
    C[Subclass B::field] --> B

When to apply (Smells)

  • Duplication: Same field in methods of sibling classes.

Motivation

  • DRY: Eliminate duplicate fields.

Mechanics (Steps)

  1. Inspect candidate fields (same name, same type).
  2. Create field in superclass.
  3. Remove fields from subclasses.

Explanation

Problem

Subclasses handle the same data.

Solution

Move the data to the superclass.

Pros and Cons

Pros Cons
  • Less duplication
  • Might need access modifier changes (private -> protected)
  • Code example

    Typescript

    Before

    After

    PHP

    Before

    After