This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
Private Members in a Superclass
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
which is why this works (printing "1")
So are private variables actually always inherited, but it's just that you can't access them if they're private?
Say I have some huge private object like an image file declared in the superclass
are all my ClassD instances going to be huge as well?
My main method creates an instance of ClassB, which uses the print method that it inherited from ClassA. But this is expected behaviour according to the tutorial. You can access private variables of the superclass if there's a method that uses them. But my question is, what if there isn't.
BTW it doesn't make any difference in which class the main method is located.
A subclass inherits all the member variables and methods from its superclass. However, the subclass might not have access to an inherited member variable or method. For example, a subclass cannot access a private member inherited from its superclass. One might say, then, that the item was not inherited at all. But the item is inherited.
The first edition of Mugal & Rasmussen's, A Programmer's Guide to Java Certification said that private members "are still inherited, but they are not accessable in the subclasses." Then they changed their minds (with an interesting qualification) for the second edition: "Private members are not accessible from any other class. This also applies to subclasses... Since they are not accessible by simple name in a subclass, they are also not inherited by the subclass."
Members of a class that are declared private are not inherited by subclasses of that class.
So I would say, conceptually yes. Private members of a superclass are "part of" a subclass instance, and are reachable (as you've demonstrated). However, I would avoid the word "inherited," because the JLS is clear about this.