• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inheritance

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,
I read from some materials that all members of a class are inherited. Is it really true for private members.
If it does, that means the subclass will also contain the private member from the superclass. Therefore, the subclass should be able to access it, say from within the subclass itself.
Actually, it is not. When I access the private member from within the subclass. The compiler will not let me go.
Can anybody clear my brain?
Regards,
Gigi.
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The subclass inherits everything the superclass has, that's true. However, don't confuse that with members' accessibilities modifiers (public, protected, none <default>, private).
Althought the subclass does inherits everything, but that doesn't mean it can access private members from superclass. Accessibilities will determine whether your subclass have access tot he superclass members or not.
 
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gigi:
It is important to seperate out the issue of access privileges from functionality. When one class inherits another, it gains all of the functionality contained in the superclass. Thus, if B inherits A, B inherits all of A's functionality, including the private aspects of A. (In other words, all of A is present in B whether or not some of A is private.) However, B cannot, itself, access the private elements of A. The use of the private members of A is restricted to the members of A. This makes sense because if a subclass were allowed access to the private elements of a superclass, it would render encapsulation virtually meaningless.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, I believe that the JLS states that private members are not inherited.


8.2.1.3 Inheritance with private
In the example:

the class variable totalMoves can be used only within the class Point; it is not inherited by the subclass Point3d. A compile-time error occurs because method move of class Point3d tries to increment totalMoves.


Of course, the difference between "not-inherited" and "its-there-but-you-can't-ever-access-it" is really non-existant; both situations have teh same effect on the programmer.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joel McNary:
In fact, I believe that the JLS states that private members are not inherited.

It doesn't. section 8.2.1.3 is specifically talking about private class (i.e. static) variables, and that's something else altogether. Private instance variables are inherited. After all, an instance of the subclass contains all variables used in its ancestor classes. The fact that these variables may not be directly accessible doesn't make any difference, they are still part of the object and can be manipulated through the superclasses' API.
- Peter
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joel: In fact, I believe that the JLS states that private members are not inherited.
Peter to Joel: It doesn't. Section 8.2.1.3 is specifically talking about private class (i.e. static) variables, and that's something else altogether. Private instance variables are inherited.
Well, 8.2 Class Members does say that "Members of a class that are declared private are not inherited by subclasses of that class." The reason that we have a difference of opinion here is because we are talking about two different concepts, as Herb mentioned. I believe JLS should be a bit more precise to avoid the ambiguity between access priviliges and object composition when it comes to inheriting private members of a superclass.
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oooh, you're right. I love discussions that look technical but in the end turn out to come down to simple semantical misunderstanding I agree that the JLS use of the word "inherited" is too imprecise: for example, private static members are "not inherited" in a completely different way than private instance members.
Thanks for the clarification, well spotted.
- Peter
 
Herb Schildt
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joel:
If I correctly understand what you mean, then I must respectfully disagree with your statement:
"Of course, the difference between "not-inherited" and "its-there-but-you-can't-ever-access-it" is really non-existant; both situations have teh same effect on the programmer."
To my way of thinking, there is a fundamental difference. The private portions of a superclass must be present in a subclass object. Otherwise, the code in the superclass that utilizes those private members would not work in the subclass object. Therefore, given that the entire functionality of a superclass is present in a subclass, how did it get there? That answer: it was inherited when the subclass extended the superclass. What other word could we use to describe this process other than "inherited"?
Finally, I believe that the proper interpretation of the JLS on this point is that access to a private member is not inherited. Clearly, the functionality must be present. I also agree with Peter and Eugene that the JLS should remove this ambiguity from its description because it is misleading.
[ October 13, 2003: Message edited by: Herb Schildt ]
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Herb,
Nice to see you here in javaranch. Complete Refernce was the first Java book that I read.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Herbert,
Its really nice to be able to even write to you. Yes Complete reference has been my first book in java too. Ever since its first version to the latest I have them all.... kind of a BIBLE for Java. I know this is not where we review books but the book has changed my life and especially helped me in the beginning of my programming career ... so much that your name now confirms the book will be easy to understand and cover most of the concepts too ... be it Java/C++
Can you please let me know if you have any seminars or events that you may be attending (North East Coast of US). I would love to meet you in person
Your biggest Fan ever
Karen
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Karen Gomes:
Hi Herbert,
Its really nice to be able to even write to you. Yes Complete reference has been my first book in java too. Ever since its first version to the latest I have them all.... kind of a BIBLE for Java. I know this is not where we review books but the book has changed my life and especially helped me in the beginning of my programming career ... so much that your name now confirms the book will be easy to understand and cover most of the concepts too ... be it Java/C++
Can you please let me know if you have any seminars or events that you may be attending (North East Coast of US). I would love to meet you in person
Your biggest Fan ever
Karen



It is Herb and not Herbert. Herb and Herbert are 2 different guys.

http://www.amazon.com/exec/obidos/tg/detail/-/0072224207/qid=1065014268/sr=1-6/ref=sr_1_6/103-9694381-1428613?v=glance&s=books
 
Herb Schildt
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pradeep and Karen:
Thank you for your kind words. They are appreciated!
I have a little free time right now and thought that it would be fun to participate in the JavaRanch forums. I really like the "be nice" culture of JavaRanch. I enjoy helping beginners, and JavaRanch offers the perfect opportunity. Frankly, I am very impressed with the high level of knowledge at JavaRanch. This is a great Web site for anyone working in Java to find answers to their questions.
Karen: I currently have no events or seminars planned in your area at this time. But thanks for asking. However, January 6th through the 9th I will be discussing my latest book: The Art of Java and other topics right here at JavaRanch. I am really looking forward to this.
Pradeep: Actually Herb is just the short form of Herbert, so both are me. I don't know why Amazon shows it both ways. Perhaps it is to allow a search engine to find both forms.
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pradeep: Actually Herb is just the short form of Herbert, so both are me. I don't know why Amazon shows it both ways. Perhaps it is to allow a search engine to find both forms.


Thanks for claryfing! You are a great author.
I am lucky to meet you in javaranch. Today is really my lucky day!
You were the most popular author for Java When I was college and lot of guys refered your complete reference. I still have you reference book which I purchased in 1998. It is very good book.
I am looking forward for your The Art of Javabook.
Thanks for your time
 
Karen Gomes
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Pradeep it was truely our lucky days .... Will look forward to talking to Herbert during the book reviews.
Cheers
Karen
 
author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Hello, Herb!)
Back to the original question that you posted:
If a parent class has a private data member but public get/set methods for that member, then the subclass can use the inherited get/set methods to access its own private attributes:

I hope this helps!
 
reply
    Bookmark Topic Watch Topic
  • New Topic