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

Interfaces implicitly public?

 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

As per my understanding, all the methods & variables in an interface are implicitly public, but the interface itself May have public or package access. Is that correct?

So here:


i & myMethod() are implicitly public but Bounceable has package access.

Am I correct?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're somewhat correct.

Methods in a interface are implicit public and variables are implicit public static and final!
Non-inner interface may have default or public access. However an inner interfaces may
have all accesslevels.

 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:However an inner interfaces may
have all accesslevels.



Thanks Wouter!! Glad to know that I was correct on the first part. But... what are inner interfaces used for. I know about the uses of inner classes, but inner inetrfaces???
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An example of an inner interface is the java.util.Map.Entry interface. A public interface.
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, perfect example! Thanks Wouter
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad I could help.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what are inner interfaces used for. I know about the uses of inner classes, but inner inetrfaces???

They make sense when the interface is only used in conjunction with the class.
Map.Entry point out by Wouter is a good example. The Entry interface is only associated with the Map, therefore they define it as nested interface. If Map.Entry defines as separate interface, then people can refer to Map.Entry outside of the context of a Map. Define Entry as part of a Map provides better understanding of relationship.

Oops... Too late...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic