• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Overriding question

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic