Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy - can a method defined in an Interface have default values?

If the following is entered in Eclipse/STS (with groovy):

interface iFaceWithAnIssue {
    def thisIsFine(a,b,c)
    def thisHasProblems(alpha='va')
}

The only line that complains is the one trying to use a default value. I can not tell from the codehaus site if this is supported or not.

The IDE error is:

Groovy:Cannot specify default value for method parameter 

So this makes me think it is not supported. As there will be multiple implementations, I wanted to use an interface here. I don't really need the default value in the interface, but there is an error trying to fulfill the interface contract if the implementation class then tries to default this argument. Is there any way?

like image 944
JoeG Avatar asked Jan 20 '14 16:01

JoeG


People also ask

Can we have default method in interface?

Interfaces can have default methods with implementation in Java 8 on later. Interfaces can have static methods as well, similar to static methods in classes. Default methods were introduced to provide backward compatibility for old interfaces so that they can have new methods without affecting existing code.

How do I set default value in groovy?

In groovy you can assign default values to method's arguments, making those arguments optional. Usually the optional arguments are the trailing ones, and that makes sence from the POV of calling such methods. You could also declare default values for the middle arguments, like you did.

What is default method in interface?

Default methods enable you to add new functionality to existing interfaces and ensure binary compatibility with code written for older versions of those interfaces. In particular, default methods enable you to add methods that accept lambda expressions as parameters to existing interfaces.

Can we have multiple default methods in interface?

Multiple Defaults With default functions in interfaces, there is a possibility that a class is implementing two interfaces with same default methods. The following code explains how this ambiguity can be resolved. First solution is to create an own method that overrides the default implementation.


Video Answer


1 Answers

No, you cannot.

When you define a default value, Groovy actually creates multiple methods in your class, so for example:

class Test {
    void something( a=false ) {
        println a
    }
}

Actually creates

public void something(java.lang.Object a) {
    this.println(a)
}

and

public void something() {
    this.something(((false) as java.lang.Object))
}

This can't be done as it stands in Interfaces.

You could do:

interface iFaceWithAnIssue {
    def thisHasProblems()
    def thisHasProblems(alpha)
}

Then

class Test implements iFaceWithAnIssue {
    // This covers both Inteface methods
    def thisHasProblems(alpha='va') {
        // do something
    }
}
like image 153
tim_yates Avatar answered Oct 25 '22 12:10

tim_yates