Programming Search⌘K

Techniquecore

Push down method

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

Family
refactoring
Intent
Keep a method in the class whose behavior it is.

Purpose

A method in a superclass is a part of the interface of each subclass. If only one subclass has a use for it, the other subclasses carry behavior that does not belong to them. A caller can call the method on an object where it makes no sense.

Push down method moves the method to the subclass that needs it. The catalogue card has get quota in Employee, with the subclasses Engineer and Salesman. After the refactoring, get quota is in Salesman only.1 The card names the inverse, Pull Up Method.1

Steps

  1. Copy the method into each subclass that needs it.
  2. Remove the method from the superclass. Test.
  3. Remove the method from each subclass that does not need it. Test.

A caller must know that it has an object of the subclass. If some callers call the method through a variable of the superclass type, the method cannot move down. In that case, keep it, or change those callers first.

Example

Only salesmen have a sales quota. Before the refactoring, an engineer also had a quota property.

import assert from "node:assert/strict";

class Employee {
  constructor(name) { this._name = name; }
  get name() { return this._name; }
}

class Engineer extends Employee {}

class Salesman extends Employee {
  constructor(name, quota) { super(name); this._quota = quota; }
  get quota() { return this._quota; }
}

const ana = new Salesman("Ana", 50000);
const bo = new Engineer("Bo");
assert.equal(ana.quota, 50000);
assert.equal("quota" in bo, false);
console.log(ana.name, ana.quota);
Output
Ana 50000
Language What the example shows
JavaScript quota moved from Employee to Salesman, as above
Python the same move; hasattr shows that an engineer no longer has a quota
Go not inheritance: the method moves from the embedded struct to the one type that needs it
PythonGitHub
class Employee:
    def __init__(self, name: str) -> None:
        self.name = name


class Engineer(Employee):
    pass


class Salesman(Employee):
    def __init__(self, name: str, quota: int) -> None:
        super().__init__(name)
        self._quota = quota

    @property
    def quota(self) -> int:
        return self._quota


ana, bo = Salesman("Ana", 50000), Engineer("Bo")
assert not hasattr(bo, "quota")
print(ana.name, ana.quota)
Output
Ana 50000
package main

import "fmt"

type Employee struct{ Name string }

type Engineer struct{ Employee }

// Quota is a method of Salesman only, not of the embedded Employee.
type Salesman struct {
	Employee
	quota int
}

func (s Salesman) Quota() int { return s.quota }

func main() {
	ana := Salesman{Employee{"Ana"}, 50000}
	_ = Engineer{Employee{"Bo"}} // Engineer has no Quota method.
	fmt.Println(ana.Name, ana.Quota())
}
Output
Ana 50000

When to use

  • Only one subclass uses the method. The other subclasses ignore it, or they override it to throw an error.
  • A subclass does not want what it inherits. This is Refused bequest. Push down method is the first fix for it.
  • Before you replace the subclass. A method that is in the right subclass is easier to move into a delegate. See Replace Subclass with Delegate.

Pitfalls

Polymorphic callers. Code that holds a list of employees and asks each one for its quota needs the method on the superclass. Then the method belongs there, perhaps with a default value for the other subclasses.

Many subclasses need it. If most subclasses need the method, pushing it down makes copies. Keep it in the superclass, or add an intermediate class.

Tools that do it

IntelliJ IDEA has Push Members Down. Its documentation says: "The Push Members Down refactoring lets you clean up the class hierarchy by moving class members to a subclass or a subinterface."4 Most IDEs for Java and C# have a similar action.

The catalogue lists the refactoring in the first edition, the second edition and the Ruby edition, with the tag dealing-with-inheritance.2 In the second edition it is in Chapter 12, Dealing with Inheritance.3

References

  1. Martin Fowler, Catalog of Refactorings, Push Down Method, the entry, the quota sketch; inverse of Pull Up Method
  2. Martin Fowler, Catalog of Refactorings, index, the entry pushDownMethod, its books and tags
  3. Martin Fowler, Refactoring, Improving the Design of Existing Code, second edition, Addison-Wesley, 2018, Chapter 12, Dealing with Inheritance, Push Down Method
  4. JetBrains, IntelliJ IDEA documentation, Pull members up, push members down, Push members down

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/ZF7RND/text.yaml