0

Maybe it is the late hours :) But can any one tell why parent class does pull variables from the child

Foo {
   public String myString = "My test string of Foo"

   public printOutString () {
       println this.myString
   }
}

Bar extends Foo {
   public String myString = "My test string of Bar"
}

Foo.printOutString() //prints out "My test string of Foo" as expected

Bar.printOutString() //prints out "My test string of Foo" as not expected thought it would take the String from Bar instead 
Dasma
  • 1,023
  • 13
  • 34

1 Answers1

5

There is no field inheritance in Groovy nor in Java. You can override the value of the field, as the linked question's answer suggest:

class Foo {
   public String myString = "My test string of Foo"

   public printOutString () {
       myString
   }
}

class Bar extends Foo {
   { myString = "My Bar" }
}


assert new Foo().printOutString() == "My test string of Foo"
assert new Bar().printOutString() == "My Bar"
Community
  • 1
  • 1
Will
  • 14,348
  • 1
  • 42
  • 44