File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access modifiers

 
Archana Nagaraj
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
Firefox Browser Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic