File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface doubt" Watch "Interface doubt" New topic

Interface doubt

Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189
This question is from the abhilash free mock exam :

Question 6.
An Interface can never be private or protected.



The response is False, but that is not what I was thinking. I thought that interface can be either public or default (but never private or protected).
Is this an error ?

SCJP 5 (76%)
SCWCD 5 (86%)
SCBCD 5(70%)
"The greatest glory in living lies not in never falling, but in raising every time we fall.".. Nelson Mandela
Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

Interfaces can be public or default. You are correct

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
The answer depends on the kind of interface: A top level interface can only be public or default. But an inner interface can have any access modifier.

It's the same as with a class: A top level class can only be public or default, but an inner class can have any access modifier.

SCJP 5 (98%) - SCBCD 5 (98%)
Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189
Thanks guys,

The problem is that the question was too ambiguious, because it was not precised wether it's an inner or top level interface.

kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
Even i am not to create inner Interface as private or protected also.. i am try to do like following as per Ralph Jaus ,

is it valid.

public interface OuterInterface {

private interface InnterInterface{


i am getting compile Time error.

kaushik Vira
SCJP, Preparing SCWCD..
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi kaushik,

generally: within an interface everything is (implicit) public. Therefore you can't use the modifiers private and protected in your sample.

The following works:
[ July 21, 2008: Message edited by: Ralph Jaus ]
Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

kaushik ,You can not declare the member of the interface as private or prtected or default if you does not provide any access modifier that does not mean it is default, the members of the interface are always I correct Ranchers.
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
Oh Ya, it`s working as you both explain. did you give me any link where i can get detail information about this concept and practically where it`s useful to hide visibility of the methods and system Design
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Access modifiers are treated in K&B chapter 1 and any java tutorial book.

Practically: Think of a helper method of a public method. If the helper method would be public,too, everyone could use it (if you like or not). If you then want to change the parameter list of your helper method, for example, other classes may be affected. But if your helper method is private, nobody else can use it and you don't have to care about any other classes. So your system has less changes and becomes more stable.
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 122
Can someone post the code where an interface can be declared private or protected? I wasn't able to create one... getting compile error!
That would just help me understand better!... Thanks in advance
[ July 21, 2008: Message edited by: Milan Sutaria ]

SCJP 6 83%
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
A few post above you'll find a code sample.
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Are inner interfaces an objective of the exam?
By the way, here is an example of private, protected, default and public interfaces:

[ July 22, 2008: Message edited by: Raphael Rabadan ]

SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
is it correct?

private and protected modifiers can be used for inner interfaces only and

public and default can be used for top level interfaces.

or else correct me.

SCJP5 and SCWCD1.5
Think Twice Act Wise...
Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

Yes Ganesh you are right....

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Don't get me started about those stupid light bulbs.
subject: Interface doubt
It's not a secret anymore!