JAVA subclass accesses parent class protected scope member variables

protected scope supports access to this package and subclasses

parent class Public Class A {

protected String name = "zs";

}

subclass:

Public Class B extends A {

public void test () {

System.out.print (name);

/ / Why does it not support access in this way of new A () .name? isn"t this also called access in a subclass? }

}

Aug.25,2021

if you are accessing this field in the same package, this access support is supported. If you cross the package, you won't be able to access it.


is actually accessible as long as you access the current instance 's own protection variable, rather than another instance of someone else's variable, regardless of whether that person is also surnamed Zhao or not.

you can take a look at this blog
http://www.blogjava.net/NewMo.
and this
https://stackoverflow.com/a/3.

.
Menu