• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private variables aren't inherited?

 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Java Tutorial on Inheritance it says
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?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You did not use a in classB, You only call the print method. The print method belongs to ClassA and use a variable.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to JLS - 6.4.3 The Members of a Class Type...
Members [of a class] are either declared in the type, or inherited because they are accessible members of a superclass or superinterface which are neither private nor hidden nor overridden (ยง8.4.8).
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are not inherited as in they cannot be accessed.
They are inherited as in there is memory reserved for them. So yes, ClassD instances will also be huge.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luigi Plinge wrote:...are private variables actually always inherited, but it's just that you can't access them if they're private? ...

Conceptually, yes.

In fact, some authors phrase it that way. For example, The Java Tutorial says...
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."

However, the "definitive" Java Language Specification - 8.2 states...
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.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing that up!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic