• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding question

 
Sachin Kapoor
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vikram Shinde
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Vikram Shinde
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic