• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Ranch question about private methods

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The java ranch question #20:
Can a private method of a superclass be declared within a subclass?
Java Ranch Answer: Yes
"Trick question! Private methods can NOT be overridden, but they can be redeclared/redefined in the subclass, and called directly on the subclass object. But polymorphism will not apply"
Kathy and Bert Book on Objective 1.2 Private Members page 74, second and third paragraph:
"A private member is invisible to any code outside the member's own class...When a member is declared private, a subclass can't inherit it."
Maybe a better question is:
"Can a method with the same name as a private superclass method be declared in a subclass?"
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But heck, that would give it away .
The real exam will word it that it can not be inherited, but that it can be declared.
I think that both are fine the way that they are worded.
Actually we have discussed the wording of that concept several time in detail and this is the way that it was decided on.
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can a private method of a superclass be declared within a subclass?
Java Ranch Answer: Yes


this is correct!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually we have discussed the wording of that concept several time in detail and this is the way that it was decided on.
Got a link? Seems to me Leo's right - the wording we have just doesn't make sense. "Can a private method of a superclass be declared within a subclass?" No, but we can declare a new method with the same signature.
that would give it away
As opposed to obfuscating the answer with self-contradictory terminology?
 
reply
    Bookmark Topic Watch Topic
  • New Topic