O'Reilly logo

Programming Groovy 2 by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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​ ​void​ raise(java.math.BigDecimal ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required