2

I have a problem with calling an overriden method from java class. I have the following Java class:

public class Base
{
    int state = 0;
    public void called()
    {
        System.out.println("Hello, from called method: " + state);
    }

    public String getFirst() 
    {
       return "From Base;
     }

    //
    ...
    //
}

I use a groovy script to override getFirst() that so that it calls called()

def base = [ getFirst : {
    called()                    // this line has an error
    "From Second"
    }] as Base

base.getFirst()

How do I implement the this?

1 Answer 1

2

You can't use the proxy magic in that way... At the time of the Maps declaration, it doesn't know it's going to be a Proxy for Base, so it will throw the error

Why not just do it the normal way?

 def base = new Base() {
   public String getFirst() {
     called()
     "from me"
   }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Hm, it works. Thanks, I googled over one hour and find that overriding in groovy implemented only with closures.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.