This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Private variables aren't inherited? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Private variables aren Watch "Private variables aren New topic
Author

Private variables aren't inherited?

Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

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

Joined: Sep 06, 2007
Posts: 635


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

Joined: Jan 06, 2011
Posts: 441

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

Joined: Aug 31, 2004
Posts: 11343

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).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Jan 06, 2011
Posts: 441

Thanks for clearing that up!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Private variables aren't inherited?
 
Similar Threads
Question regarding encapsulation
Overridding abstract methods
Subclass calls super class constructor then how many object will be created
Is my constructor doing more than I am aware of ?