Community
Participate
Working Groups
Accessing a protected field in a super-class from outide of the package the super-class was defined in, results in Eclipse claiming that 'the field <...> is not visible'. Tried it on 3.0M8 and it doesn't work, but everythings ok on the 2.1.2 build: Example of problem: package packageA; public abstract class BaseClass { protected BaseClass parent; protected int myField; protected abstract myMethod(); } package packageA.subpackage; public class DerivedClass { protected myMethod() { int a = parent.myField; } } Here Eclipse claims that myField is not visible. Is this correct behaviour?
Your test case was incomplete. The following one does issue a compile error which is indeed expected. package packageA; public abstract class BaseClass { protected BaseClass parent; protected int myField; protected abstract void myMethod(); } package packageA.subpackage; import packageA.BaseClass; public class DerivedClass extends BaseClass { protected void myMethod() { int a = parent.myField; } } myField is a protected field, and thus can only be used from within same package or type hierarchy as long as it is applied to itself or a subclass (jls 6.6.2.1).
Eclipse 2.1.3 did also correctly report this problem either.