Suppose you download a jar file,
project-a.jar. You know that this jar contains a public class
abc.PublicClass which you plan to use in your own project.
It happens that internally to the
project-a.jar the
abc.PublicClass extends a private package class
abc.PackageHiddenClass which contains a protected field.
Here is the code for
abc.PackageHiddenClass:
And here's the code for
abc.PublicClass:
Now suppose in your own project you write code which creates a variable of type
abc.PublicClass and you try to access the protected field:
I understand that it will not compile but the compiler message threw me off a little:
$ javac -cp deps/project-a.jar xyz/Test.java
xyz/Test.java:8: error: protectedField has protected access in PackageHiddenClass
System.out.println(publicClass.protectedField);
^
1 error
I had this notion that a protected member is "inherited" by a subclass so I expected that the compiler will say that "protectedField has protected access in
PublicClass".
Question: should I change the way I think about protected members of a superclass?