• Post Reply Bookmark Topic Watch Topic
  • New Topic

access modifiers of nested interface  RSS feed

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

TopLevel.java:3: error: illegal combination of modifiers: public and private
private interface Nested1
^
TopLevel.java:6: error: illegal combination of modifiers: public and protected
protected interface Nested2
^
2 errors

I read in book that top level interface can have only public or default access modifiers although nested interface can have private,protected,public access modifiers, but it seems not true ?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meeta gaur wrote:I read in book that top level interface can have only public or default access modifiers although nested interface can have private,protected,public access modifiers, but it seems not true ?

Where did you read that? And did the book specifically give the example of an interface nested in an interface?

You can certainly have protected and private interfaces nested in a class, but your Nested1 example just doesn't make any sense.
What code would ever be able to use it?

Also: ALL members of an interface are public by default. Remember?

Winston
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All members of interfaces, and that includes nested interfaces, are implicitly public (even if you don't use the 'public' keyword on them). You can't have private or protected members in an interface. Reference: JLS secion 6.6.1.

meeta gaur wrote:I read in book that top level interface can have only public or default access modifiers although nested interface can have private,protected,public access modifiers, but it seems not true ?

If the interface is nested inside a class it can be public, protected, private or have default access, but if it's nested inside another interface it will always be public.
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
meeta gaur wrote:I read in book that top level interface can have only public or default access modifiers although nested interface can have private,protected,public access modifiers, but it seems not true ?

Where did you read that? And did the book specifically give the example of an interface nested in an interface?

You can certainly have protected and private interfaces nested in a class, but your Nested1 example just does make any sense.
What code would ever be able to use it?

Also: ALL members of an interface are public by default. Remember?

Winston


Ah ! got it.Incomplete fact.Thanks Winston and Jesper

Book
untitled.JPG
[Thumbnail for untitled.JPG]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!