liveBook · Manning

Polymorphism

As explained in Data-Oriented Programming: Reduce software complexity

Polymorphism is a language capability in which a common interface can represent values or objects with different implementations.

Polymorphism lets one common interface represent values or objects with different implementations. An OOP design gives each animal a greet method, so dogs, cats, and cows respond differently while callers use the same interface. The useful question is not whether inheritance is required, but where the variation belongs. section 13.1

A data-oriented design can preserve the same variation without class inheritance:

  • Represent each animal as a generic map containing type and name. section 13.1
  • Dispatch on the type field, using a switch or a multimethod. section 13.1 section 13.6
  • Validate the map at development time before selecting behavior. section 13.1
  • Add implementations independently, keeping the dispatch mechanism decoupled from method implementations. section 13.6

Animals with one greeting interface

An IAnimal interface exposes greet. Dog prints “Woof,” cat prints “Meow,” and cow prints “Moo.” The map-based alternative stores {type, name}, then selects the matching greeting function. Multimethods extend this pattern with defaults, multiple dispatch, or runtime-dependent dispatch. section 13.1 section 13.6

Listing 13.1 OOP polymorphism illustrated with animals
interface IAnimal {
public void greet();
}
 
class Dog implements IAnimal {
  private String name;
  public void greet() {
    System.out.println("Woof woof! My name is " + animal.name);
  }
}
 
class Cat implements IAnimal {
  private String name;
  public void greet() {
    System.out.println("Meow! I am " + animal.name);
  }
}
 
 
class Cow implements IAnimal {
  private String name;
  public void greet() {
    System.out.println("Moo! Call me " + animal.name);
  }
}

Compare the interface-and-class version of polymorphism directly with its animal implementations. section 13.1

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Explains interfaces, type dispatch, validation, multimethod structure, and dispatch variants without descending into implementation internals.
Examples
Uses dog, cat, and cow greetings, type-based switches, animal maps, schemas, and separate greeting functions.
What it assumes
Assumes familiarity with functions and basic OOP, while teaching polymorphism and data-oriented alternatives through concrete animal examples.
What you can do after
Builds polymorphic behavior with interfaces, type dispatch, or multimethods while evaluating inheritance and data-oriented trade-offs.
How it connects
Connects interfaces, class inheritance, generic maps, multimethods, single dispatch, multiple dispatch, and dynamic dispatch.

Also explained in

Compare how 9 books explain polymorphism →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming: Reduce software complexity. The section links point to the parts of the book it draws on.