views:

1851

answers:

7

Hi all,

I know that an interface must be public. However, I don't want that.

I want my implemented methods to only be accessible from their own package, so I want my implemented methods to be protected.

The problem is I can't make the interface or the implemented methods protected.

What is a work around? Is there a design pattern that pertains to this problem?

From the java guide, an abstract class wouldn't do the job either.

Please help, guys, I'd really appreciate it.

jbu

A: 

You can go with encapsulation instead of inheritance.

That is, create your class (which won't inherit anything) and in it, have an instance of the object you want to extend.

Then you can expose only what you want.

The obvious disadvantage of this is that you must explicitly pass-through methods for everything you want exposed. And it won't be a subclass...

Michael Haren
+5  A: 

read this.

"The public access specifier indicates that the interface can be used by any class in any package. If you do not specify that the interface is public, your interface will be accessible only to classes defined in the same package as the interface."

Is that what you want?

duffymo
I think there's a little bit of confusion in the question between the visibility of the interface itself, and the visibility of the interface methods. You can make the interface non-public, but the methods will always be public.
Jon Skeet
+5  A: 

You class can use package protection and still implement an interface:

class Foo implements Runnable 
{
    public void run()
    {
    }
}

If you want some methods to be protected / package and others not, it sounds like your classes have more than one responsibility, and should be split into multiple.

Edit after reading comments to this and other responses:

If your are somehow thinking that the visibility of a method affects the ability to invoke that method, think again. Without going to extremes, you cannot prevent someone from using reflection to identify your class' methods and invoke them. However, this is a non-issue: unless someone is trying to crack your code, they're not going to invoke random methods.

Instead, think of private / protected methods as defining a contract for subclasses, and use interfaces to define the contract with the outside world.

Oh, and to the person who decided my example should use K&R bracing: if it's specified in the Terms of Service, sure. Otherwise, can't you find anything better to do with your time?

kdgregory
Good point! +1 while the interface is public, the implementation could be "default" access modifier. It seems to me this is the right answer! :)
OscarRyz
I do not understand this answer. Since run is public, it can be called from any package, right? I don't want my implemented method to be called from other packages.
jbu
but the class you're calling it from is only visible in the package. you're calling run on a particular instance, which isn't public.
duffymo
Oh, I see. Yes, I think this is what I'm looking for.Thanks guys.
jbu
Hmm, this isn't really safe. If your package allows a Foo to escape, anyone can reference it as a Runnable and invoke the run method.
erickson
true, but if letting objects "escape" is an issue with your application, then you need to revisit the design
kdgregory
+2  A: 

Here's how it could be done using abstract classes.

The only inconvenient is that it makes you "subclass".

As per the java guide, you should follow that advice "most" of the times, but I think in this situation it will be ok.

public abstract class Ab { 
    protected abstract void method();
    abstract void otherMethod();
    public static void main( String [] args ) { 
        Ab a = new AbImpl();
        a.method();
        a.otherMethod();
    }
}
class AbImpl extends Ab {
    protected void method(){
        System.out.println( "method invoked from: " + this.getClass().getName() );
    }
    void otherMethod(){ 
        System.out.println("This time \"default\" access from: " + this.getClass().getName()  );
    }
}
OscarRyz
A: 

I would just create an abstract class. There is no harm in it.

Hash
How can you say that?! abstract classes are the root of all evil! :D
piggles
+3  A: 

When I have butted up against this I use a package accessible inner or nested class to implement the interface, pushing the implemented method out of the public class.

Usually it's because I have a class with a specific public API which must implement something else to get it's job done (quite often because the something else was a callback disguised as an interface <grin>) - this happens a lot with things like Comparable. I don't want the public API polluted with the (forced public) interface implementation.

Hope this helps.

EDIT: Also, if you truly want the methods accessed only by the package, you don't want the protected scope specifier, you want the default (omitted) scope specifier. Using protected will, of course, allow subclasses to see the methods.

EDIT2: BTW, I think that the reason interface methods are inferred to be public is because it is very much the exception to have an interface which is only implemented by classes in the same package; they are very much most often invoked by something in another package, which means they need to be public.

Software Monkey
A: 

This question is based on a wrong statement:

I know that an interface must be public

Not really, you can have interfaces with default access modifier.

The problem is I can't make the interface or the implemented methods protected

Here it is:

C:\oreyes\cosas\java\interfaces>type a\*.java
a\Inter.java
package a;

interface Inter {
    public void face();
}

a\Face.java
package a;

class Face implements Inter {
    public void face() {
        System.out.println( "face" );
    }
}


C:\oreyes\cosas\java\interfaces>type b\*.java
b\Test.java

package b;
import a.Inter;
import a.Face;

public class Test {
    public static void main( String [] args ) {
        Inter inter = new Face();
        inter.face();
    }
}

C:\oreyes\cosas\java\interfaces>javac -d . a\*.java b\Test.java
b\Test.java:2: a.Inter is not public in a; cannot be accessed from outside package
import a.Inter;
        ^
b\Test.java:3: a.Face is not public in a; cannot be accessed from outside package
import a.Face;
        ^
b\Test.java:7: cannot find symbol
symbol  : class Inter
location: class b.Test
        Inter inter = new Face();
        ^
b\Test.java:7: cannot find symbol
symbol  : class Face
location: class b.Test
        Inter inter = new Face();
                          ^
4 errors

C:\oreyes\cosas\java\interfaces>

Hence, achieving what you wanted, prevent interface and class usage outside of the package.

OscarRyz