3.6 Multimethods
Dynamic typing and dynamic languages change how objects respond to method calls.
Groovy supports polymorphism, like Java does, but it goes far beyond simply dispatching methods based on the target object’s type. Let’s look at polymorphism in Java:
TypesAndTyping/Employee.java | |
| // Java code |
| public class Employee { |
| public void raise(Number amount) { |
| System.out.println("Employee got raise"); |
| } |
| } |
The Employee
class’s
raise
method
simply reports that it was called.
Now look at the Executive
class:
TypesAndTyping/Executive.java | |
| // Java code |
| public class Executive extends Employee { |
| public void raise(Number amount) { |
| System.out.println("Executive got raise"); |
| } |
| |
| public ... |
Get Programming Groovy 2 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.