JDK-4030982 : Protected isn't!
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 1.1
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_95
  • CPU: x86
  • Submitted: 1997-02-07
  • Updated: 1997-07-22
  • Resolved: 1997-07-22
Related Reports
Relates :  
Description

Name: mc57594			Date: 02/07/97


Given the following 3 pieces of java src:

A class has access to a protected field that it shouldn't have access to!

Given the following inheritance hierarchy all declared in either the same package or default unnamed package

    class ProtectedBase
        protected setValue()   ---\__
             |                       \_____________
             |                                     class ExtendsProtect extends ProtectedBase        class Protect
  protected int anotherValue                         
  protected setValue                           

I have observed an instance of class Protect can do two things
it shouldn't be able to do!

given an instance of class Protect, which in turn has an instance of ExtendsProtected 
1) Protect can see the protected field anotherValue
2) Protect can call method setValue that was overloaded in ExtendsProtect

Hopefully this all makes sence....

Also a co-worker of mine appears to be able to gen classes
that can access another classes private and protected fields when
the two different classes are not even related in the inheritance
hierarchy! This stuff all verifies and runs! (I'll file a 
different bug report for that one!)


Here's the src that reproduces the same problem.

    +1  
    +2  public class ProtectBase
    +3  {
    +4  protected int value=0;
    +5  
    +6  public ProtectBase()
    +7      {
    +8      value=10;
    +9      }
   +10
   +11  protected void setValue()
   +12      {
   +13      value=20;
   +14      }
   +15  }


    +1  
    +2  public class ExtendsProtect extends ProtectBase
    +3  {
    +4  protected int anotherValue=0;
    +5  
    +6  public ExtendsProtect()
    +7      {
    +8      anotherValue=11;
    +9      }
   +10
   +11  protected void setValue()
   +12      {
   +13      value=30;
   +14      }
   +15  }


    +1  
    +2  public class Protect extends ProtectBase
    +3  {
    +4  
    +5  public void
    +6  testProtection(ExtendsProtect e)
    +7      {
    +8      e.setValue();
    +9      System.out.println("value is: "+e.value);
   +10      System.out.println("anotherValue is: "+ e.anotherValue);
   +11      }
   +12
   +13  public static void main(String args[])
   +14      {
   +15      Protect p=new Protect();
   +16      ExtendsProtect e=new ExtendsProtect();
   +17      p.testProtection(e);
   +18      }
   +19  }

Output is:

value is: 30
anotherValue is: 11

I believe the correct output for value should be 20, and anotherValue
should not be allowed to be accessed...

company  -  IBM  , email  -  ###@###.###
======================================================================

Comments
WORK AROUND Name: mc57594 Date: 02/07/97 None. ======================================================================
11-06-2004

EVALUATION Why is this a bug? According to the JLs, protected members are freely accessible inside the same package. sheng.liang@Eng 1997-07-22
22-07-1997