Subclasses inherit private instances of variables from superclasses

Do subclasses inherit private fields?

This question concerns the same problem, but I don’t quite understand how it satisfies the (seemingly) contradictory situations below.

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

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

Does this mean that it does not inherit private instance variables and private methods?

However, how does it work if it inherits a public access method from its parent? Does it return an instance variable that it does not know?

In addition, my book on computer science (Baron AP Computer Science A) has the correct answer to a question that says that "(Subclass) inherits all private instance variables and public access methods from (Superclass)."

Is this not related to the oracle textbook?

thanks for the help

+6
source share
6 answers

Private members are also inherited. To test this, you can do the following in a 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 an 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.

0
source

The accessory will work fine. Remember that the accessor is launched in the "context" of the superclass and therefore the accessory will be able to see the member that is hidden from subclasses.

As for the tutorial, it depends on your point of view. A subclass inherits private members in the sense that they are actually inside instances of the subclass (so they will occupy memory, etc.), but the subclass will not be able to access them directly.

+10
source

Think like an onion. Each level of the hierarchy is a layer inside the bow. For example, if class C extends class B, which extends class A, then an object of class C will look like this:

Class C Object

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

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

However, he may contact the public and protected members B and A.

Thus, the public access function for B will either allow this class C object to access the private instance variable of class B or class A part of the "object."

+4
source

"Does not inherit" means that you cannot access it. It still exists, it just cannot interact in any way (with the exception of calls to superclass impersonal methods).

The accepted answer in a question related to you explains this. Is there any specific part of this explanation that is unclear?

+2
source

Of course, when you create an object of class B that inherits from class A , if class A has private elements, according to Java access rules, you cannot access them, but these elements exist in private memory, even as null references if they are null objects.

In the Java Language Specification you can read:

A class inherits from its direct superclass and direct superinterfaces all non-grafted fields of the superclass and superinterfaces, which are both available for code in the class, and not hidden in the class.

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

+1
source

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

+1
source

Source: https://habr.com/ru/post/915139/


All Articles