File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Interfaces

 
Poushi Poushi
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everybody,

I am new to java and while I was going through it i read that all the members inside an interface should be public.

Can anyone please tell me why we cannot give any other access modifier other than public inside an interface to method or variable.
and please don't write like it is the way java accepts i knew that but want to know why ?

wht's the logic behind that. Why cannot it be protected ?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33691
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Poushi,
An interface is meant to be a contract with the caller. Public access is also related to a contract. Protected is related to system internals.
 
Bill Shirley
Ranch Hand
Posts: 457
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no technical reason there couldn't be, but as Joanne states it would go against the design reasons for Interfaces.

Providing an abstract class with a (sometimes abstract) protected method that should be overriden is already a mechanism available when programming.

Interfaces are for defining method implementation similarities outside of an inheritance hierarchy.

A protected method would only be invokable by subclassing, and an interface is not needed to impart that information.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic