Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Interface

 
Reshma Shanbhag
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have a question regarding Interfaces. Can an Interface be private or protected??.
I tried working with top level interfaces .. the complier complains. But i get to hear that inner Interfaces can be private or protected. I have no much idea about inner interfaces.. and i dont know what i tried was correct .
I tried the following example
interface Check_Inner_Interface{

void aMethod();

private interface Inner_Private{

}

protected interface Inner_Protected{

}

}
When i try to compile i get the following message ..
Check_Inner_Interface.java:5: illegal combination of modifiers: public and private
private interface Inner_Private{
^
Check_Inner_Interface.java:9: illegal combination of modifiers: public and protected
protected interface Inner_Protected{
^
2 errors
It would be of help if u help me to clear this confusion.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you did is "almost" correct. You can indeed have private/protected inner interface but only in classes.
If you declare an interface within another interface it will automatically be public since all members of an interface must be public (and are made implicitely public) otherwise what good would be an interface if some of its members can be private? When you compile your code you get the "illegal combination of modifiers" thing, because the compiler automatically adds a public modifier to all members within the interface and an interface cannot be public and private at the same time. Makes sense, doesn't it?
Hope this clears your doubts.
[ January 29, 2003: Message edited by: Valentin Crettaz ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic