1

if I have two methods in a java class like below, the public method can be overridden but not the private one.

public class JavaClass {
    public void echo() { 
        System.out.println("Echo'd from Java Class"); 
    }

    private void doSomethingElse() {
        System.out.println("Something else happening from Java Class");
    }
}

If I try to override it like so:

JavaClass pojo = new JavaClass()
pojo.metaClass.echo = {
    println "Echo overridden!"
}

pojo.metaClass.doSomethingElse = {
    println "This won't be overridden"
}
println pojo.echo
println pojo.doSomethingElse

The private method is not overridden.

POJO's use the expandoMetaClass which is, from my understanding, associated to the Java class through the MetaClass Registry. The ExpandoMetaClass should have all method calls delegated to it. I can see in debugger that the ExpandoMetaClass is there and the new method is defined within the metaClass, but it's not being used! What is happening here?

Edit: Note I am not asking IF i can, I know I can't. I'm asking why it doesn't work. It works for public methods as you can see from the example above if you run it.

jacob
  • 398
  • 4
  • 13
  • No, I got this question from that thread. I'm explicitly asking why, not if I can – jacob Nov 23 '17 at 02:35
  • You no where pointed the reference then? leading as if the question is yours. – Rao Nov 23 '17 at 02:37
  • I don't think it's relevant because I'm asking a different question with a different example. It doesn't elaborate my question by referencing it. – jacob Nov 23 '17 at 02:39

0 Answers0