Name: krC82822 Date: 05/08/2001
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
Interfaces are very useful, but I often want to write methods that have package
(default) access instead of public access.
For example, I have a viewer class that can place any one of many custom
components in its main view. The viewer then asks this newly-added component
which menu items should be enabled. This provides a simple, powerful
implementation of context-sensitive menus. I do this through an interface, which
all the components implement. For example, the interface could look like this:
package com.myfirm.mywidget.ui;
import java.awt.Component;
interface MenuStates // interface has package access
{
Component getViewer(); // I want these methods to have package access, too
boolean isViewEnabled();
boolean isCopyEnabled();
boolean isSortEnabled();
...
}
Any class that implements this can provide a viewer for my main window. However,
there is no reason why any of these methods need to be visible outside the
current package. I want to encapsulate them inside the package, and I especially
want to hide the getViewer method. The current specification for Java doesn't
allow this.
I can't achieve this through an abstract class, as the evaluator of 4144491
suggests, because the classes that implement MenuStates all extend other AWT
classes. These methods need to be in an interface.
Please don't close this as a duplicate of 4144491. That was asking for protected
access, which is clearly not a good idea. Package access is different. It's
essentially a limited version of public access. Methods should only be allowed
to have package access if the interface itself has package access. A public
interface should certainly require public methods. But right now, I can write an
interface with package access, but I'm forced to give its methods public access.
This makes no sense.
(Review ID: 124020)
======================================================================
|