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








