Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: corrected example on visibility of static protected fields

...

Code Block
class A{
  static private int numBananas= 0
  static private String sayBananas(int n){
    "There are ${numBananas= n} bananas!"
  }
  static protected int numApples= 0
  static protected String sayApples(int n){
    "There are ${numApples= n} apples!"
  }
}
class B extends A{
  static testAccesses(){
    assert sayBananas(37) == 'There are 37 bananas!'
    //numBananas //compile error when uncommented:
                 //A's private field not visible here

    assert sayApples(29) == 'There are 29 apples!'
    //numApples //compile error when uncommented:
                //A's protected field not visible here in an inheriting class
  }
}
assert B.sayBananas(31) == 'There are 31 bananas!'
try{ B.numBananas; assert 0 }catch(e){ assert e in MissingPropertyException }
assert B.sayApples(23) == 'There are 23 apples!'
assert B.numApples == 23

B.testAccesses()

...