Java is the future !: Quiz of the Day | November 4, 2010

Thursday, November 4, 2010

Quiz of the Day | November 4, 2010

Given the code below, which access modifiers (public, protected, or private) can legally be placed before the myMethod() method on line 3, if no other changes are made to the code? If line 3 is left as it is, which keywords can legally be placed before the myMethod method on line 8?

a. private or nothing (default) on line 3. Nothing (default) or protected or public on line 8. 
b. public or protected on line 3. private or nothing (default) on line 8. 
c. Nothing (default) or protected or public on line 3. private or nothing (default) on line 8. 
d. public on line 3 and private on line8.


Solution : Choice A is the correct answer.

The basic principle is that a method cannot be overridden to be more private. Since the method is being overridden to be friendly (default access / no modifier), it can only be private or friendly in the superclass. Secondly, if the method in superclass is left as it is (i.e., friendly access), the method in subclass can be friendly, protected, or public. 

Source : "Facebook community"

No comments:

Post a Comment