Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple Q, I'm newbie

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic