Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces

 
dude philips
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Friends,

I am DudePhilips from India,a Gradute, started career in Java. I have a doubt regarding Interfaces. " Y interfaces can't be declared as protected?"

can any body please let me know?

:roll:
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its by design. An interface is the point of communication between one program entity and any other program entity. If you restrict access to an interface, its no longer really an interface, if you follow?
 
Chris Staten
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would we want to declare an interface as protected? If you declare an interface as protected then only the other classes in the same package (or subclasses of the interface) could access it. With an interface the whole point is to allow ANY class to implement it so that the implementing class is guaranteed to have the exact method signature as any other class that is implementing the interface. They allow us to to get around the problems associated with multiple inheritance, but stil have that method signature guarantee. this way we know how to call the method and what to expect in return.

Besides, what�s to protect in an interface? None of the methods do anything until the interface is implemented and they are themselves implemented.
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface can actually be declared as protected or private.

See 9.1.1 of the JLS: http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html#78582

The access modifiers protected and private pertain only to member interfaces within a directly enclosing class declaration
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic