Summary: | Java Compiler Error with method visibility | ||
---|---|---|---|
Product: | [Eclipse Project] JDT | Reporter: | James Howe <bugzilla> |
Component: | Core | Assignee: | JDT-Core-Inbox <jdt-core-inbox> |
Status: | RESOLVED INVALID | QA Contact: | |
Severity: | major | ||
Priority: | P3 | ||
Version: | 2.1 | ||
Target Milestone: | 3.0 M1 | ||
Hardware: | PC | ||
OS: | Windows XP | ||
Whiteboard: |
Description
James Howe
2003-05-27 11:36:03 EDT
I just tried this example with jdk 1.4.1 & it reports the same error we do. D:\temp>javac p2/B.java p2/B.java:8: mySomething() has protected access in p1.A return ((A ) anA).mySomething(); ^ 1 error Yup, you are correct that JDK 1.4.1_02 does report an error. However, I still fail to see why it is an error. The method being invoked is marked as 'protected' which should mean that it is visible to items in the package as well as to subclasses in any package. I'm casting the parameter which comes in as an 'InterfaceA' to be a 'TypeA'. An instance of TypeA clearly has access to this method. If I move TypeB to the same package as TypeA, the compiler doesn't object. I fail to see why it should matter what package TypeB resides in, particularly when it isn't an instance of TypeB which is calling the method in question. Even if it was, the method should be visible because TypeB is a subclass of TypeA. This behavior is described in the JLS 6.6.2 (also check example in 6.6.7). Basically protected methods can be used in subclasses TypeB on receivers of type TypeB (or subtype), not on supertype TypeA. So: 'this.mySomething()' would be allowed. |