We know polymorphism is a fundamental concept in Java and groovy supports this concept as well but in enhanced way, do you think how? let us take an example:


class MultiMethodDemo{

    int multiMethod(String arg){
        return 1
    }

    int multiMethod(Object arg){
        return 2
    }

    public static void main(String[] args) {

        MultiMethodDemo multiMethodDemo = new MultiMethodDemo()
        Object obj = "object"
        multiMethodDemo.multiMethod(obj)
    }
}

If we run this code in java(then please put the semicolon to each statement):

result will be 2

and if we run the same code in groovy the result will be 1 since groovy is smart 😉 .

If we have overloaded methods in a class,then groovy call the method not only on the basis of target object(the object on which method is called) but it also check the parameter as opposed to Java in which methods are chosen at compile time.As in groovy the method-dispatching is based on multiple objects “the target and the
parameters” this is called multiple dispatch or multimethods.