aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Simple Q, I'm newbie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Simple Q, I Watch "Simple Q, I New topic
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Simple Q, I'm newbie