• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Inheritance issue.

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic