• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring an Interfaces protected

 
Ast Lavond
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello together,

you can declare an Interface public and with default acces (accessed only if the class is in the same package).
Why isn't it possible to declare an Interface protected, so that only classes and interfaces in the same package or interfaces which extends other interfaces, can use the members of the protected interface?

kind regards
lavond
 
Larry Olson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will leave it to the experts to provide more explanation/details about this. But my understanding is that all top level classes and interfaces in java could only have public or default access. protected and private is not allowed. So what you are saying is also applicable to top level classes. Sometimes it seems like we have to accept things blindly without questioning them...unfortunately. But I hope someone will have a more convincing answer.
 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I agree with you Larry.
 
Muneeswaran Balasubramanian
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lavond,
Because we need to implement the interface from out of the package also.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ast Lavond wrote:
Why isn't it possible to declare an Interface protected, so that only classes and interfaces in the same package or interfaces which extends other interfaces, can use the members of the protected interface?


*Suppose* Interface A(which is declared as protected) is in under foo package . Interface B(which needs A to extends) is in bar package . so can B extends A ? No, A wont visible in Interface B package . to make it visible A needs to be public .

IMO, declaring toplevel classes and Interface as private or protected wont make sense
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Larry Olson wrote: Sometimes it seems like we have to accept things blindly without questioning them...unfortunately.


Partly , I Agree . If you want to play cricket, you need to follow certain rules without arising question
 
Ast Lavond
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
many thanks for the replys.

Simran Dass wrote:
Because we need to implement the interface from out of the package also.


That's why i'm confused, because the default access (which is provided for classes and interfaces) allows only package visibility. So why not a protected access?

Larry Olson wrote:
Sometimes it seems like we have to accept things blindly without questioning them...unfortunately.


You're right. Either public access or default access, a in-between is not needed.
 
Muneeswaran Balasubramanian
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lavond,
We can make our inner interface as a private or protected.The top level elements are only public or default.For ex,


Try this we achieve the interface property.
Cheers Munees

MyBlog
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic