• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance issue.

 
Neil Mac
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, slightly grey area in my understanding of inheritance...
A class "SuperClass" has a sub-class "SubClass" and SuperClass defines a private method.
SubClass inherits this method (as it inherits all of the super class) but can't access it and no object of type SubClass can access this method through the SubClass interface.
How can we say that SubClass inherits this method if it knows nothing about it and can't access it ? What use is this ?
Hope you can help clear up my lack of understanding on this topic.
Thanks.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class "SuperClass" has a sub-class "SubClass" and SuperClass defines a private method. SubClass inherits this method (as it inherits all of the super class)...
Private data members amd methods are not inherited. �6.6.8 Of Java Language Specification: A private class member or constructor is accessible only within the class body in which the member is declared and is not inherited by subclasses.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. The reason to make a method private is precisely to prevent anybody, inlcluding derived classes, from getting to them. If you want to allow derived classes but nobody else, see "protected".
 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the preceding replies, I would add this comment:
Assume a superclass called X and a subclass called Y. Y contains all of the functionality of X plus whatever functionality Y adds. Thus, if X contains a private method called priv(), the functionality provided by priv() is still present in the X portion of Y. Its just that Y cannot call priv() because it is private to X. Thus, Y does inherit the whole functionality of X, but not the ability to access X's private members.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch "Spider Man",
We don't have a lot of rules around here. In fact the one big rule is "Be nice." But there's another one that requires you to use a display name that is either your real name or looks like a real name. Remember the JavaRanch naming policy that you read when registering?
Please take a minute to change your display name to show two names, preferably real names. It's all about maintaining our professional image - don't let's the one-eyed moose fool you!
Hope you'll be visiting the ranch often,
Cindy
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree to the other postings, but like to give an other point of view.
A private method from class Super may only be called from methods in Super.
If Sub extends Super, Sub may call a method from Super which calls the private Method.
The method is available but only indirect.
You may use this technique, to hide methods, which must be called carefully, so the public Method can assure, that some conditions are fullfilled, before the method is called.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic