Java OCA OCP Practice Question 518

Question

For the following code:

1. package abcde; 
2. 
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 abcde; 
 2. /*from ww w  . java  2s.c  om*/
 3. class MyClass extends abcde.Bird { 
 4.   public void fly() { 
 5.     /* MyClass-specific flight code. */ 
 6.   } 
 7.   public int getRefCount() { 
 8.     return referenceCount; 
 9.   } 
10. } 
  • A. Compilation of MyClass.java fails at line 4 because method fly() is protected in the superclass, and classes Bird and MyClass are in the same package.
  • B. Compilation of MyClass.java fails at line 4 because method fly() is protected in the superclass and public in the subclass, and methods may not be overridden to be more public.
  • C. Compilation of MyClass.java fails at line 7 because method getRefCount() is static in the superclass, and static methods may not be overridden to be nonstatic.
  • D. Compilation of MyClass.java succeeds, but a runtime exception is thrown if method fly() is ever called on an instance of class MyClass.
  • E. Compilation of MyClass.java succeeds, but a runtime exception is thrown if method getRefCount() is ever called on an instance of class MyClass.


C.

Note

Static methods may not be overridden to be nonstatic.

B is incorrect because it states the case backward: methods can be overridden to be more public, not more private.




PreviousNext

Related