Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing private and public/protected methods of the same name causes multimethods to be disabled and is forbidden to avoid surprising behaviour

I just upgraded one of my Grails apps to 2.2.0 which is using Groovy 2.0 and I am now getting this compile error:

Mixing private and public/protected methods of the same name causes multimethods to be disabled and is forbidden to avoid surprising behaviour. Renaming the private methods will solve the problem.

Based on the following code:

def getRootDomain(key) { }

private getRootDomain() { }

It's an easy fix but I'd really like to understand the why better. Can someone explain this to me?

like image 461
Gregg Avatar asked Jan 15 '13 13:01

Gregg


People also ask

What's the difference between a protected method and a private method?

Both protected and private methods cannot be called from the outside of the defining class. Protected methods are accessible from the subclass and private methods are not. Private methods of the defining class can be invoked by any instance of that class. Public access is the default one.

Should methods be public or private?

Generally you should expose as little as possible and make everything private that is possible. If you make a mistake and hide something you should be exposing, no problem, just make it public.

Can a private class have public methods?

However, although the syntax allows private classes to have public methods, it won't increase the visibility of those methods sufficiently to be visible outside the containing class.

Is it good practice to make all methods public?

Yes it is very bad practice - you're letting your tools make design decisions for you. I think the main problem here is that you're trying to treat each individual method as a unit. This is generally the cause of all unit test woes.


1 Answers

It's because of the way groovy chooses which method to call, and its potential incompatibility with java.

Have you seen this thread on the mailing list?

http://groovy.329449.n5.nabble.com/mixing-public-private-overloaded-methods-causes-compilation-error-td367147.html

like image 192
tim_yates Avatar answered Sep 19 '22 12:09

tim_yates