GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Access modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Access modifiers" Watch "Access modifiers" New topic
Author

Access modifiers

Archana Nagaraj
Greenhorn

Joined: Aug 08, 2012
Posts: 5
Hi,

Could you guys please help me understand this question and the answer chosen . This is one of the question in Whizlabs mock tests

Question: 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?

1. class HumptyDumpty
2. {
3. void myMethod(){}
4. }
5. class HankyPanky extends HumptyDumpty
6. {
7. void myMethod(){}
8. }

Choices:
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.

Choice A is the correct answer.

My doubt/question here is How can we make a method "private" in the base class(HumptyDumpty) which is meant for overriding in the subclass(Hanky Panky)? If we do so, we can't even see the method(marked private in base class) in its subclass!






sarvesh dikonda
Ranch Hand

Joined: Apr 08, 2012
Posts: 58

Hello Archana. The answer to the question you asked is indeed option 'a'. Now, your doubt is also correct, but lets stick to the question asked in the given problem -

Question: 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?


So, option 'a' correctly answers the given scenario. No need to worry about the OVERRIDING as in this case if line 3 is made private(with the rest of code intact) then, it won't be visible in the child class(As you already pointed out) and therefore, it won't be considered as an OVERRIDE nor an OVERLOAD.

Hope this helps!

Always believe in yourself
 
GeeCON Prague 2014
 
subject: Access modifiers