8

Do subclasses inherit private fields?

This question addresses the same problem but I don't quite understand how that satisfies the (seemingly) contradictory situations below.

http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Says that "A subclass does not inherit the private members of its parent class."

This means that it neither inherits private instance variables nor private methods right?

However, how does this work if it inherits a public accessor method from its parent? It returns an instance variable that it doesn't know exists?

Also, my computer science book (Baron's AP Computer Science A) has the correct answer to a question that says that "The (Subclass) inherits all the private instance variables and public accessor methods from the (Superclass)."

Isn't this in contraction to oracle's tutorial?

Thanks for your help

Community
  • 1
  • 1
Ian
  • 541
  • 3
  • 8
  • 15

6 Answers6

10

The accessor will work fine. Remember that the accessor runs in the "context" of the superclass and so the accessor will be able to see the member that's hidden from the subclasses.

As for the textbook, it depends on your point of view. The subclass inherits the private members in the sense that they are actually there inside instances of the subclass (so they'll take up memory, etc.), but the subclass will be unable to directly access them.

QuantumMechanic
  • 13,239
  • 3
  • 38
  • 65
  • 3
    +1 Yes, your answer can be emphasized by an example: Humans inherited (if we believe in evolution theory) some kind of tail from monkeys (a part of the bone in the bottom) but they can't wag their tail. They can only watch monkeys as they wag their tale, or to give them banana to encourage them to wag their tale. – Lajos Arpad May 08 '12 at 01:12
  • And how does this relate to the inheritance of private methods? Are they inheritted but hidden? You can't use (object of subclass).(private method of superclsas) right? edit: Lajos that is a great example. – Ian May 08 '12 at 01:19
  • 1
    It is related in the fact that the tail is a private member of monkeys, wagging the tale is a private method, because only monkeys have tails and only they can wag their tails. However, there are two methods: the first is the public method of watching the tail of monkeys, which is defined in the base class (Monkey) and can be accessible to humans too, who are also able to watch monkeys wagging their tale, while the second is the private method of giving banana to the monkeys, which is only accessible for humans, because monkeys are selfish and they don't give banana to each-other. – Lajos Arpad May 08 '12 at 01:24
  • I've also given an answer, where you can test this. – Lajos Arpad May 08 '12 at 01:24
  • And wag tail is a private method of monkey? And therefore not inherited by human? – Ian May 08 '12 at 01:36
  • It is inherited, but can't be accessed directly. You can only access the method of tail wagging indirectly, through a method accessible by you. In our case this is the method of giving a banana to the monkey, which causes the monkey to enter a state of hapyness and wag his tail. This is called indirect access. – Lajos Arpad May 08 '12 at 01:39
5

Think of it like an onion. Every level of hierarchy is a layer within the onion. For example, If class C extends Class B, which extends class A then an object of class C would look like:

Object of Class C

       -------------------------------------------------------------
       |                                                           | 
       |                   C and it's members                      |
       |                                                           |
       |    ------------------------------------------------       |
       |    |                                              |       |
       |    |              B and it's members              |       |
       |    |    ------------------------------------      |       |                                              
       |    |    |         A and it's members       |      |       |
       |    |    |                                  |      |       |
       |    |    ------------------------------------      |       |                                   
       |    ------------------------------------------------       |
       |                                                           |
       -------------------------------------------------------------            

So, an object of class C does have members of B and A. But it cannot access private members of B and A.

It can however access public and protected members of B and A.

So a public accessor function of B or A would allow this object of class C to access a private instance variable of the class B or class A "part" of the object.

j0k
  • 21,914
  • 28
  • 75
  • 84
Anup Cowkur
  • 19,813
  • 6
  • 48
  • 82
2

"Doesn't inherit" here means that you cannot access it. It still exists, just not in any way that you can interact with (except through calling non-private methods of the superclass).

The accepted answer in the question you linked to does explain that. Is there anything particular part of that explanation that isn't clear?

Community
  • 1
  • 1
trutheality
  • 21,548
  • 6
  • 47
  • 65
1

Certainly when you create an object of a class B that inherits from a class A if the class A has private items, according to the rules of access in Java, you can not have access them, but these elements do exist in private memory, even as null references if they were null objects.

In The Java Language Specification, you can read:

A class inherits from its direct superclass and direct superinterfaces all the nonprivate fields of the superclass and superinterfaces that are both accessible to code in the class and not hidden by a declaration in the class.

A private field of a superclass might be accessible to a subclass - for example, if both classes are members of the same class. Nevertheless, a private field is never inherited by a subclass.

Paul Vargas
  • 38,878
  • 15
  • 91
  • 139
1

If the subclass is in same package it inherits private members also otherwise it inherits public and protected members only!

0

Private members are inherited too. To test this, you can do the following in the superclass:

//...
private String myText = "I'm in the superclass";

private void setMyText(String myTextValue)
{
    this.myText = myTextValue;
}

public void setMyTextPublic(String myTextValue)
{
    setMyText(myTextValue);
}

public String getMyText()
{
    return myText;
}
//...

Create a method in the inherited class:

//...
public void setMyTextInTheSuperClass(String myTextValue)
{
    System.out.println(getMyText());
    setMyTextPublic(myTextValue);
    System.out.println(getMyText());
}

public void setConstantValueToMyText()
{
    setMyTextInTheSuperClass("I am in the child class");
}
//...

And call setConstantValueToMyText somewhere.

Lajos Arpad
  • 45,912
  • 26
  • 82
  • 148
  • 1
    I'm all for metasyntactic naming schemes, but at a certain point it can get convoluted. You should probably rename `foobar` to `setFoo` and `barfoo` to `getFoo`. – Jeffrey May 08 '12 at 00:59
  • @Jeffrey, you are absolutely right in pointing out the naming issue, however the namings in the test code were without any sense on purpose to show the op, who is probably a novice that we are talking about any member and any function, not just setting or getting values. However, you are right when you say about these namings that "don't try this at home, your namings should make sense". – Lajos Arpad May 08 '12 at 01:06
  • I assumed that most people would have the sense not to name their methods like that in real code, but when I tried to follow your example and I was mildly confused. I can only imagine how confusing it would be for a beginner. – Jeffrey May 08 '12 at 01:08
  • Fair enough, the code is modified. Maybe it's more helpful now. – Lajos Arpad May 08 '12 at 01:18
  • Thanks, but you can't directly manipulate myText in the inherited class right? – Ian May 08 '12 at 01:34
  • 2
    Yes, you are right. If you try to manipulate myText directly from the inherited class, your code won't compile. – Lajos Arpad May 08 '12 at 01:40