For the following code:
1. package abcde; 2. // w w w . j av a 2s . co m 3. public class Bird { 4. protected static int referenceCount = 0; 5. public Bird() { referenceCount++; } 6. protected void fly() { /* Flap wings, etc. */ } 7. static int getRefCount() { return referenceCount; } 8. } Which statement is true about class Bird and the following class MyClass?
1. package singers; 2. /*from www .j a va 2s. c o m*/ 3. class MyClass extends abcde.Bird { 4. MyClass() { referenceCount++; } 5. 6. public static void main(String args[]) { 7. System.out.print("Before: " + referenceCount); 8. MyClass florence = new MyClass(); 9. System.out.println(" After: " + referenceCount); 10. florence.fly(); 11. } 12. }
fly()
is protected in the superclass. fly()
is protected in the superclass. A.
There is nothing wrong with MyClass.
The static referenceCount
is bumped twice: once on line 4 of MyClass and once on line 5 of Bird.
The no-argument constructor of the superclass is implicitly called at the beginning of a class's constructor, unless a different superclass constructor is requested.
Because referenceCount
is bumped twice and not just once, answer B is wrong.
C says that statics cannot be overridden, but no static method is being overridden on line 4.
D is wrong since protected is the access modifier you want Bird.fly()
to have: you are calling Bird.fly()
from a subclass in a different package.
Answer E is wrong.