aspose file tools*
The moose likes Java in General and the fly likes Inheritance issue. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inheritance issue." Watch "Inheritance issue." New topic
Author

Inheritance issue.

Neil Mac
Greenhorn

Joined: Oct 03, 2003
Posts: 7
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

Joined: Oct 08, 2001
Posts: 2937
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

Joined: Jan 29, 2003
Posts: 8791
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".


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
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
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

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.


http://home.arcor.de/hirnstrom/bewerbung
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inheritance issue.