Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface doubt

 
Mamadou Touré
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is from the abhilash free mock exam :

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

Answers

True
False


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 ?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces can be public or default. You are correct
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mamadou Touré
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Regards
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 180
Eclipse IDE Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 public.am I correct Ranchers.
 
kaushik vira
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ralph Jaus
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few post above you'll find a code sample.
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Ganesh you are right....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic