This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
internet detective

Joined: May 26, 2003
Posts: 30136

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.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, 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);
It is sorta covered in the JavaRanch Style Guide.
subject: Regarding Interfaces
Similar Threads
Class defined inside an Interface
why variables inside interface has to final
doubt : class defined inside an interface
Abstract class and interfaces.