• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Ranch question about private methods  RSS feed

 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • 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?"
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • 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.
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a private method of a superclass be declared within a subclass?
Java Ranch Answer: Yes

this is correct!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!