aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding question 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 "Overriding question " Watch "Overriding question " New topic
Author

Overriding question

Sachin Kapoor
Greenhorn

Joined: Jul 01, 2007
Posts: 19
Question is: 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?

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

Options are

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.

The correct answer is A. However, I am confused because placing private on line 3 means a private method is being overridden, which is not possible. So how can A be correct?
Marcelo Ribeiro
Greenhorn

Joined: Aug 04, 2007
Posts: 9
Hello,

From Kathy's book page 30:

What about a subclass that tries to inherit a private member of its superclass?
When a member is declared private, a subclass can't inherit it. For the exam, you
need to recognize that a subclass can't see, use, or even think about the private
members of its superclass. You can, however, declare a matching method in the
subclass. But regardless of how it looks, it is not an overriding method!


so at line 8 you're free to add either protected or public modifier.

Thanks
Sachin Kapoor
Greenhorn

Joined: Jul 01, 2007
Posts: 19
Gotcha. So myMethod in subtype does not override but is unrelated to myMethod in supertype. If access modifier for myMethod in supertype is default (no modifier), and the two classes are in the same package, then the two methods are related and it's an override.

Originally posted by marcelo ribeiro:
Hello,

From Kathy's book page 30:



so at line 8 you're free to add either protected or public modifier.

Thanks
Mohammad Hossain
Greenhorn

Joined: Jul 26, 2007
Posts: 14
As far as I know. Its not overriding. I mean HankyPanky class doesnot know anything about HumptyDumpty's myMethod(). So in HankyPanky its HankyPanky's myMethod(). I mean from HankyPanky's point of view there is no myMethod() in
HamptyDumty so it is writing its own method.


Live Free Or Die
Vikram Shinde
Greenhorn

Joined: Jul 26, 2007
Posts: 21
Originally posted by Sachin Kapoor:
Question is: 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?

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

Options are

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.

The correct answer is A. However, I am confused because placing private on line 3 means a private method is being overridden, which is not possible. So how can A be correct?


SCJP 1.5<br />Preparing for SCWCD
Vikram Shinde
Greenhorn

Joined: Jul 26, 2007
Posts: 21
if I make private void myMethod() {}
in HankyPanky
then
myMethod() in HankyPanky cannot override myMethod() in HumptyDumpty; attempting to assign weaker access privileges; was package

but if I make private void myMethod() {}
in HumptyDumpty
then
it is invisible to subclass as it is private
so its not overriding but its a redeclaration of method or just a declaration.

and If i make default / public / protected void myMethod(){}
in HumptyDumpty
then I am attempting to assign less privileges to overriding method
so its possible
 
Don't get me started about those stupid light bulbs.
 
subject: Overriding question