aspose file tools*
The moose likes Beginning Java and the fly likes Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

Ron Lam
Greenhorn

Joined: Sep 22, 2003
Posts: 6
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.
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
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.
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
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.


For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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
John Smith
Ranch Hand

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

Joined: Apr 20, 2000
Posts: 3252
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
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
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 ]
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Hi Herb,
Nice to see you here in javaranch. Complete Refernce was the first Java book that I read.


Groovy
Karen Gomes
Ranch Hand

Joined: Aug 25, 2003
Posts: 60
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

Joined: Feb 27, 2002
Posts: 8919

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
Ranch Hand

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

Joined: Feb 27, 2002
Posts: 8919

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

Joined: Aug 25, 2003
Posts: 60
Yes Pradeep it was truely our lucky days .... Will look forward to talking to Herbert during the book reviews.
Cheers
Karen
Jacquie Barker
author
Ranch Hand

Joined: Dec 20, 2000
Posts: 201
(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!


Author of Beginning Java Objects, Beginning C# Objects, and Taming the Technology Tidal Wave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance