aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface" Watch "Interface" New topic
Author

Interface

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Can the private,protected access modifiers be applied to Interfaces. How do they restrict access??
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
NagarajGoud uppala
Ranch Hand

Joined: Nov 13, 2008
Posts: 86
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


I know Life is very Tough...But I AM TOUGHER
SCJP 1.5
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Thanks..nagraju.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 31, 2004
Posts: 11343

For example...
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
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

Joined: Sep 16, 2008
Posts: 509


In the above example I am getting compiler error. What is the right way of extending an Inner class?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Thanks Ankit
 
 
subject: Interface
 
Similar Threads
access specifier and access modifier
Access specifiers & Access Modifiers
database in netbeans
Constructors
basic question on access modifiers and levels