| Author |
Simple Q, I'm newbie
|
Alberto Dal Dosso
Greenhorn
Joined: Jun 15, 2005
Posts: 2
|
|
Hi, I think this code will not compile: class Zoo{ protected String coolMethod(){ return "Wow baby"; } } class Moo extends Zoo { public void useMyCoolMethod() { System.out.println("Moo says, " + this.coolMethod()); Zoo z = new Zoo(); System.out.println("Zoo says, " + z.coolMethod()); } } class Boo { public void useMyCoolMethod() { Zoo z = new Zoo(); System.out.println("Zoo says, " + z.coolMethod()); } } But it compile, why? I think than I can't call a protected method from Boo class.
|
 |
shetal bansal
Ranch Hand
Joined: May 09, 2005
Posts: 63
|
|
Protected members are visible to all the classes in the same package. As Boo and Zoo are in the same package, protected method coolMethod() in Zoo is available to Boo. Place Boo in a different package and try compiling, u`ll get a compile time error.
|
 |
 |
|
|
subject: Simple Q, I'm newbie
|
|
|