title: Is it possible to replace groovy method for existing object?
body: <p>The following code tried to replace an existing method in a Groovy class:</p>
  
  <pre><code>class A {
    void abc()  {
       println "original"
    }
  } 
  
  x= new A()
  x.abc()
  A.metaClass.abc={-&gt; println "new" }
  x.abc()
  A.metaClass.methods.findAll{it.name=="abc"}.each { println "Method $it"}
  
  new A().abc()
  </code></pre>
  
  <p>And it results in the following output: </p>
  
  <pre><code>original
  original
  Method org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod@103074e[name: abc params: [] returns: class java.lang.Object owner: class A]
  Method public void A.abc()
  new
  </code></pre>
  
  <p>Does this mean that when modify the metaclass by setting it to closure, it doesn't really replace it but just adds another method it can call, thus resulting in metaclass having two methods?  Is it possible to truly replace the method so the second line of output prints "new"?   </p>
  
  <p>When trying to figure it out, I found that <a href="http://groovy.codehaus.org/Using+the+Delegating+Meta+Class" rel="noreferrer">DelegatingMetaClass</a> might help - is that the most Groovy way to do this? </p>