• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can the private,protected access modifiers be applied to Interfaces. How do they restrict access??
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No they can not. Only public and default are allowed. They restrict access the normal way. But why not try for yourself or look it up in the Java Language Specification?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you check the JLS, you will see that private and protected can be applied to (nested) interfaces...
The access modifiers protected and private pertain only to member interfaces within a directly enclosing class...
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For interfaces we can not apply private,protected.It must always declare as public because interface is method prototype that is all the methods are 100% abstract.the method body will be implemented in the implementation classes.

if your using private, the methods can't accessible to other classes.so we can't implement those methods in the implementation classes.

coming on to protected,the methods have accessibility to subclasses but any implementation class is not subclass to any interface.

so interface-always public
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..nagraju.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nagaraju uppala:
For interfaces we can not apply private,protected...

Again, that's not exactly true. A nested interface can be private or protected. (See the JLS link above.)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example...
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means public is the only access modifier that can be applied to a top-level Interface. For nested Interfaces, public,private,protected can be applied. Right?
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above example I am getting compiler error. What is the right way of extending an Inner class?
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhi the code that you gave tries to extend a non-static inner interface. You need an instance of the enclosing class to do that. This is allowed with the other non-static inner class as it has an enclosing class instance. But to implement it outside the enclosing class or in static member classes of the enclosing class, you need to have an explicit super call in the constructor with an enclosing class instance. So use this code

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic