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.
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.
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.
For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
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
"JavaRanch, where the deer and the Certified play" - David O'Meara
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.