• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Interface doubt

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interfaces can be public or default. You are correct
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 180
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few post above you'll find a code sample.
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Ganesh you are right....
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic