Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4060943

access to another package superclass protected constructor is permitted

XMLWordPrintable

    • sparc
    • solaris_2.5



      Name: ngC57085 Date: 06/24/97



      Java compiler (JDK1.1.3) permits to use protected constructors of the superclass
      from another package by a class instance creation expression new T(...).


      JLS (6.6.2) says:

      "
      A protected member or constructor of an object may be accessed from outside
      the package in which it is declared only by code that is responsible for the
      implementation of that object. Let C be the class in which a protected member
      or constructor is declared and let S be the subclass of C in whose
      declaration the use of the protected member or constructor occurs. Then:
                  . . .
         Otherwise, if an access is of a protected constructor:
                  . . .
            If the access is by a class instance creation expression new T(. . .),
            then the access is not permitted. (A protected constructor can be
            accessed by a class instance creation expression only from within the
            package in which it is defined.)

      "

      Next test is successfully compiled by JDK1.1.3 and successfully executed
      by JVM.

      > javac -d . name067c.java
      > javac -d . name067.java
      > javac -verify name067.name067
      4
      3
      2
      >
      ------------------------name067c--------------
      package name067.name067a;

      public class name067c {
         protected name067c (int a) { n = a; }
         public int n = 5;
      }
      --------------------------------------
      ------------------------name067--------------
      package name067;

      import java.io.PrintStream;
      import name067.name067a.name067c;

      public class name067 {
        public static void main(String argv[]) {
           System.exit(run(argv, System.out) + 95/*STATUS_TEMP*/);
        }
        public static int run(String argv[],PrintStream out) {
           name067d d = new name067d ();
           d.check(out);
           return 0;
        }
      }

      class name067d extends name067c {
         name067c d1;
         name067c d2 = new name067c(3);
         name067c d3;
         name067d () {
            super(3);
            d1 = new name067c(4);
         }
         void check (PrintStream out) {
            d3 = new name067c(2);
            out.println(d1.n);
            out.println(d2.n);
            out.println(d3.n);
         }
      }
      --------------------------------------



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

            tturnidgsunw Todd Turnidge (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: