File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Regarding Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regarding Interfaces" Watch "Regarding Interfaces" New topic

Regarding Interfaces

Poushi Poushi

Joined: Jun 24, 2008
Posts: 1
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

Joined: May 26, 2003
Posts: 33102

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
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.

Bill Shirley - bshirley -
if (Posts < 30) JavaRanchFAQ);
I agree. Here's the link:
subject: Regarding Interfaces
It's not a secret anymore!