This is a nice illustration of the expression problem, which the article itself points out toward the end. Multiple dispatch is a language-level way to solve it. Crafting Interpreters [1] has a very approachable Java-based chapter that also covers these patterns in detail.

[0] https://en.wikipedia.org/wiki/Expression_problem

[1] https://craftinginterpreters.com/representing-code.html