Package level interfaces

  • Thread starter James D Carroll
  • Start date
J

James D Carroll

I posted this previously:

*****
Could someone explain this:
interface myInterface{
void foo();
}
then...
class myClass implements myInterface{
void foo(){
'do this
}
}
I want the Interface, its methods, and the implementing class and its
methods to have "package" level permissions. But I get the complier error:
attempting to assign weaker access privileges; was public
Could someone explain this (or provide a link that does) ?
Thanks
****
I got a one answer that mentioned that when it comes to interfaces they are
automatically public and abstract. The abstract aspect I accept.

But isn't there a way to create an interface that can only be implemented by
classes in the same package? Or more genereally a way to restrict who can
implement my interfaces? Separately, could an abstract base class be
declared with "package" level permissions to achieve the same goal (though I
don't care for that, I's do it if I hafta)

Thanks,

PS: Apologies for reposting. I use Outlook Express and it "lost" the
reponses to my original post.
 
T

Tony Morris

I got a one answer that mentioned that when it comes to interfaces they
are
automatically public and abstract. The abstract aspect I accept.

Correct.
Java Language Specification 9.4
But isn't there a way to create an interface that can only be implemented by
classes in the same package?
No.

Or more genereally a way to restrict who can
implement my interfaces?
No.

Separately, could an abstract base class be
declared with "package" level permissions to achieve the same goal (though I
don't care for that, I's do it if I hafta)

Yes - a different "thing" altogether.

It appears that you have a design issue.
I suggest you state your requirements clearly so that you can be assisted
before naively wandering off in the wrong direction.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
W

Woebegone

James D Carroll said:
I posted this previously:

*****
Could someone explain this:
interface myInterface{
void foo();
}
then...
class myClass implements myInterface{
void foo(){
'do this
}
}
I want the Interface, its methods, and the implementing class and its
methods to have "package" level permissions. But I get the complier error:
attempting to assign weaker access privileges; was public
Could someone explain this (or provide a link that does) ?
Thanks
****
I got a one answer that mentioned that when it comes to interfaces they are
automatically public and abstract. The abstract aspect I accept.

But isn't there a way to create an interface that can only be implemented by
classes in the same package? Or more genereally a way to restrict who can
implement my interfaces? Separately, could an abstract base class be
declared with "package" level permissions to achieve the same goal (though I
don't care for that, I's do it if I hafta)

Thanks,

PS: Apologies for reposting. I use Outlook Express and it "lost" the
reponses to my original post.
Your original response pretty much says it all -- interfaces by definition
are public and abstract. If you accept the abstract part, what is so
difficult about the public part? If it's not visible, it's not part of an
interface. As for your second question, an abstract base class could be
declared, but what is your goal? Who would implement? It would be easier to
address your questions if you describe more fully what you're trying to
accomplish.

I trust that you are satisfied with the earlier explanation of your compiler
error: the declaration from the interface is implicitly public, but your
implementing class reduced the access privilege to package.
 
T

Tony Morris

But isn't there a way to create an interface that can only be
implemented

Well, yes, but I assume this isn't what you were asking.

// package scope interface
class interface X
{

}

I assume you were asking about the (non-)existance of package scope methods
of interfaces.
To want this indicates a design problem, rather than a language deficiency.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top