• 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

Quest. about Complete Java 2 Certification 2nd Edition Book

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question is below the code.
// -- Bird.java
package abcde;
public class Bird {
protected static int referenceCount = 0;
public Bird(){ referenceCount++; }
protected void fly() { /* Flap wings, etc */ }
static int getRefCount() { return referenceCount; }
}
// -- Nightingale.java
package singers;
class Nightingale extends abcde.Bird
{
Nightingale() { referenceCount++; }

public static void main(String args[])
{
System.out.println("Before: " + referenceCount);
Nightingale florence = new Nightingale();
System.out.println(" After: " + referenceCount);
florence.fly();
}
}
I'm studying the certification book "Java 2 Certification" 2nd edition and I am aware
of all the typos in the book. I looked on the errata page and it didn't mention
anything about the question i'm about to ask. On page 101 it asks which statement
is true about the code that I provided above. The answer is:
The program will compile and the output will beL Before: 0 After: 2
My questions are:
Is the statement "package singers" a typo?
If its not a typo, how can the referenceCount variable be accessed by main without
having the same package name "abcde"?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
referenceCount is protected which means that the member may be accessed by all subclasses in any packages.
 
Alfonso Harding
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You might need to keep this in mind too.
A subclass in a nother package can only access protected members in the superclass via references of ITS own type or a subtype.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic