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?
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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?
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
posted
0
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
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.