FIX: Protected Members Accessible to Subclass in Other Package

Last reviewed: January 22, 1998
Article ID: Q156912
The information in this article applies to:
  • Microsoft Visual J++ 1.0

SYMPTOMS

The Microsoft Java Compiler (JVC) incorrectly compiles code that violates field visibility rules according to the Java specification. If a sub- classed object (S) is derived from a base class (B) in another package that has a protected member variable (ld) and class S tries to access a member via a qualified name of Q.ld, access is permitted only if Q is S or a subclass of S. If Q is the base class of S, then access should not be permitted and an error should be generated. JVC compiles code that violates this field visibility rule without generating any warnings or errors.

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. This problem has been fixed in Visual J++ 1.1.

MORE INFORMATION

Steps to Reproduce Problem

The following sample code demonstrates an example where the qualified name Q.ld has Q being a superclass of S, instead of a subclass. According to the Sun specification 6.6.2, the expression a.myProtectedVariable is not valid since it is defined in another package with protected field visibility:

   // aProtectedPackage.myBaseClass
   package aProtectedPackage;
   public class myBaseClass {
      protected int myProtectedVariable;
   }

   // derivedClass
   import aProtectedPackage.myBaseClass;
   class derivedClass extends myBaseClass {
     void test(myBaseClass a) {
         this.myProtectedVariable=1;
         a.myProtectedVariable=0;  // this should generate an error
                                   // according to 6.6.2, but JVC does not
                                   // generate any warnings.
     }
   }

REFERENCES

For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, see the following page on the Microsoft Technical Support site:

   http://support.microsoft.com/support/visualj/
   http://support.microsoft.com/support/java/

Keywords          : CmdLnUtils
Technology        : kbInetDev
Version           : 1.0
Platform          : WINDOWS
Issue type        : kbbug
Solution Type     : kbfix


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 22, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.