Frage

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

War es hilfreich?

Lösung

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.

Andere Tipps

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.

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.

"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?

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.

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top