• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Q, I'm newbie

 
Alberto Dal Dosso
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic