Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Quest. about Complete Java 2 Certification 2nd Edition Book

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
thanks
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • 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.
 
Whatever. Here's a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic