[836] in java-interest

home help back first fref pref prev next nref lref last post

basic question on protected method

daemon@ATHENA.MIT.EDU (John Arthur December)
Sat Jul 22 18:14:21 1995

From: John Arthur December <decemj@rpi.edu>
Date: Sat, 22 Jul 1995 16:59:38 -0400
To: java-interest@java.sun.com


Given a class with a public, private, and protected method:
    
    class Box{
       Box() { }
       private void getKey() { }
       public void tryKey() { }
       protected void setKey() { }
    }

and a class with an application that creates a box
object:

    class Game {
       public static void main (String args[]) {
          Box theBox = new Box();
          theBox.tryKey(); // works OK 
          // this correctly won't compile (private): theBox.getKey(); 
          theBox.setKey(); // why does this compile?
       }
    }

Why is that protected method theBox.setKey() available
from the class Game?  I thought only subclasses
could access protected methods? (Game is not a subclass
of Box). Is there some error in the above (it compiles and runs)?
Or am I misunderstanding Java's protected methods?
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post