Do Subclasses Inherit Private Instance Variables From Superclasses

感情迁移 提交于 2019-12-21 00:53:46

问题


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


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/10491002/do-subclasses-inherit-private-instance-variables-from-superclasses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!