Home arrow static arrow Java Programming [Archive] - package private methods in package private Interface
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - package private methods in package private Interface
8 Duke Stars available
This topic has 19 replies on 2 pages.    1 | 2 | Next »

Posts:36
Registered: 2/28/02
package private methods in package private Interface  
Jun 17, 2004 4:35 AM



 
Hello,

is there anybody out there who could explain me, why it is not possible to define package private methods in a package private interface. Since it is possible to define an interface package private I would expect that this is possible for those interfaces at least.

Thanks in advance
Richard
 

Posts:186
Registered: 1/10/01
Re: package private methods in package private Interface  
Jun 17, 2004 4:47 AM (reply 1 of 19)



 
Why do you think this is impossible? I've been able to compile this:
interface Test {  void testing();}


This is a package private interface with a package private method, isn't it?
 

Posts:11,200
Registered: 7/22/99
Re: package private methods in package private Interface  
Jun 17, 2004 4:51 AM (reply 2 of 19)



 
Since it is possible to define an interface package private I would
expect that this is possible for those interfaces at least.

what would be the use of such possibility, the interface itself would not be visible from any other package and so would none of the methods.

This is a package private interface with a package private method, isn't it?

nope, the visibility defaults to public.
 

Posts:27
Registered: 4/19/04
Re: package private methods in package private Interface  
Jun 17, 2004 12:03 PM (reply 3 of 19)



 
Hi
Interfaces allow only public methods.
If the method is private, why define in the interface?
This method, if it has necessity of being private, you must create only in your class.
 

Posts:1,183
Registered: 1/23/02
Re: package private methods in package private Interface  
Jun 17, 2004 12:34 PM (reply 4 of 19)



 
This is a package private interface with a package private method, isn't it?

Every field and method of an interface gets defaulted to public. You can decompile your example and see.

Jsalonen gave the answer to OPs question.
 

Posts:31,095
Registered: 4/30/99
Re: package private methods in package private Interface  
Jun 17, 2004 1:21 PM (reply 5 of 19)



 
what would be the use of such possibility, the
interface itself would not be visible from any other
package and so would none of the methods.

This is a good question. Are there situations where you would want to define an interface for your package's private use and not let any other package use it? What harm is caused by making an interface public, since it still doesn't allow anybody to execute any of your package-private code? Personally I see no harm, but then I am lax and permissive about programming.

PC²
 

Posts:1,183
Registered: 1/23/02
Re: package private methods in package private Interface  
Jun 17, 2004 1:39 PM (reply 6 of 19)



 
What harm is caused by making an interface public, since it still doesn't allow anybody to execute any of your package-private code?

API pollution?

I am lax and permissive about programming.

Black mark. :-)
 

Posts:6,750
Registered: 1/25/04
Re: package private methods in package private Interface  
Jun 17, 2004 1:45 PM (reply 7 of 19)



 
Perhaps you might want to be sure that every instance of this interface is under your control.
 

Posts:3,258
Registered: 00-08-28
Re: package private methods in package private Interface  
Jun 17, 2004 1:50 PM (reply 8 of 19)



 
This is a package private interface with a package
private method, isn't it?

Every field and method of an interface gets defaulted
to public. You can decompile your example and see.

Jsalonen gave the answer to OPs question.

Exactly and just to add the member parameters become intrinsically "static" as well.
 

Posts:36
Registered: 2/28/02
Re: package private methods in package private Interface  
Jun 18, 2004 12:06 AM (reply 9 of 19)



 
The necessity is the following: I expose an API to the public where there are objects which can be stored temporarily. I then would like to differ the Objects by lets say Persistable which means that these objects can physically be stored somewhere. I do not want to expose this attribute and I want to add a method save(). Even if I choose the interface to be package-private the save() Method would have to be public although I do not want to expose it. The only workaround could be an abstract class in the hierarchy wich has not the flexibility an interface offers.

Regards
Richard
 

Posts:6,750
Registered: 1/25/04
Re: package private methods in package private Interface  
Jun 18, 2004 8:31 AM (reply 10 of 19)



 
I've never tried it, but can you have a nested interface in a package-protected class?
 

Posts:1,577
Registered: 00-02-25
Re: package private methods in package private Interface  
Jun 18, 2004 8:47 AM (reply 11 of 19)



 
I then would like to differ the Objects
by lets say Persistable which means that these objects
can physically be stored somewhere. I do not want to
expose this attribute and I want to add a method
save(). Even if I choose the interface to be
package-private the save() Method would have to be
public although I do not want to expose it.

Yep, that's one problem with implementing an interface ... check out the JavaDoc for JComboBox.actionPerformed() to see an example of undesired exposure (one that could've been hidden with a little work).

An alternative work-around is to make your Persistable interface a marker interface (it has no methods), and use reflection to invoke the expected save() method. It means you don't get compile-time type safety, someone could implement Persistable but not provide save(), but it's better IMO than throwing a class into the hierarchy.

I haven't checked the JCP, but it might be worthwhile throwing this request in if it's not already there, or giving it a vote if it is.
 

Posts:13,769
Registered: 00-11-29
Re: package private methods in package private Interface  
Jun 18, 2004 8:58 AM (reply 12 of 19)



 
Yes I've been wondering the same thing. I have a package protected interface but I have to make the methods public in order to implement it. It doesn't make much sense.

Perhaps the interface methods should default to the same level as the interface itself.
 

Posts:4,906
Registered: 23/07/02
Re: package private methods in package private Interface  
Jun 18, 2004 3:29 PM (reply 13 of 19)



 
Nice as it would be to redo interfaces to allow non-public methods, I doubt an RFE would get anywhere because there's potentially a lot of reflection code in the wild which assumes interface methods are public.
 

Posts:2,830
Registered: 9/1/03
Re: package private methods in package private Interface  
Jun 18, 2004 4:14 PM (reply 14 of 19)



 
I've never tried it, but can you have a nested
interface in a package-protected class?

thats a good idea ... you can put access restrictions on them when they
are inside a class, so package-private would work, you'd think...
 
This topic has 19 replies on 2 pages.    1 | 2 | Next »