Wednesday 20 February 2013

11. Given the code below, and making no other changes, which access modifiers

11. Given the code below, and making no other changes, which access modifiers
(public, protected or private) can legally be placed before myMethod() on line 3?
If line 3 is left as it is, which keywords can legally be placed before myMethod
on line 8?



Choices:
a. private or nothing(i.e. leaving it as it is) on line 3.
Nothing(i.e. leaving it as it is) or protected or public
on line 8.
b. public or protected on line 3. private or nothing(i.e. leaving it
as it is) on line 8.
c. nothing(i.e. leaving it as it is) or protected or public on
line 3. private or nothing(i.e. leaving it as it is) on line 8.
d. None of the above.



Answer:-
A is correct.
The basic principle is that a method cannot be overridden to be more private.
Since the method is being overridden to be friendly(default 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.

No comments:

Post a Comment