Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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