The Groovy subclass is called the superclass method that accesses the closure

I have a groovy superclass that looks like this:

class AGroovyClass {
   private String str = "hello"
   void printString(int nTimes) {
     nTimes.times { println str } 
  }        
}

      

and subclass

class AGroovySubclass extends AGroovyClass {
   // some other subclass methods
}

      

My client code calls:

new AGroovySubclass().printString(5)

      

And it really breaks because he says there is no such "str" โ€‹โ€‹property for AGroovySubclass

I would have thought, since the printString method is in AGroovyClass, it should have no problem accessing the "str" โ€‹โ€‹property, but I'm sure I'm wrong. If I wanted to keep "street" personal, what is the appropriate way to make it work?

+3


source to share


1 answer


This is an old bug with the private access modifier. It works if you define str protected. http://jira.codehaus.org/browse/GROOVY-2433



edit: Can you avoid the closure, use a for loop instead? Not so cool, but it works :)

+5


source







All Articles