4

I know that Java interface variables are by default public static and final.

I understood why they are static and final by seeing this discussion

But I didn't reason behind why interface variables are public by default.

If any one knows the reason behind why interface variables are always public and why can not we have a protected or default variables in an interface, kindly explain me.

Kindly don't ask me to google for it because I have been googling for it for more than two hours and there is no clear explanation from any site about why the variables are public by default.

I have seen the below blog also but there is no clear info about my query.

Community
  • 1
  • 1
Jagadeesh
  • 862
  • 7
  • 23
  • 1
    Sorry @JordiCastilla. See my latest edit. Some typing mistake. – Jagadeesh Oct 06 '15 at 07:58
  • 2
    What other accessibility would you want and do you have a concrete scenario where it actually makes sense? – Damien_The_Unbeliever Oct 06 '15 at 07:59
  • @Damien_The_Unbeliever, I don't have any concrete scenarios. As I am a trainer, while I give training my participants might ask such questions. And I want to get indept knowledge on this particular concept. I thought it might be useful to others as well if I post a question regarding on this. – Jagadeesh Oct 06 '15 at 08:01
  • `private` - every implementation has to implement a function that nobody can call. `protected` - every implementation has to implement a function that only that implementation can call. Can you see a use for either of these? – Damien_The_Unbeliever Oct 06 '15 at 08:11
  • Because that's the way they designed it. What kind of an answer are you expecting, exactly? – user207421 Oct 06 '15 at 08:31
  • @EJP as I am a trainer I can not simply tell my participants "Because that's the way they designed it. What kind of an answer are you expecting, exactly?" like you said in the comment. I already explained what was my question and I don't know the answer. Because I don't know the answer that is why I posted a question here. Instead of giving answer strangely you are asking me the answer. I encourage you to see my latest edit where I clearly pointed out my question. I request you to think about the answer and if you get one kindly post it. So that helps me and lot others as well. – Jagadeesh Oct 07 '15 at 05:42

3 Answers3

4

Interfaces are "behaviour blueprints". Unlike classes, they shouldn't (and do not) have states.

Since instance variables represents the state of an object, interfaces do not have instance variables (being them private or public). And since private static variables represent the state of a class, they don't have private static variables either.

javatutorial
  • 1,916
  • 15
  • 20
  • It make sense prior to Java8. Since Java8 supports default function definitions within interfaces, IMO, having private static variables makes sense. – Dakshinamurthy Karra Oct 06 '15 at 08:58
  • You're right. Still, IMO, since default definitions were mainly a way to maintain backward compatibility, using static variables as states shouldn't be abused. It's no surprise that Java 8 "default definitions" is one of its most controversial features, as it can be easily abused resulting in messy code. – javatutorial Oct 06 '15 at 09:05
0

an interface has functions which the using class needs to communicate with the outside. Through that you can have a few different classes which all have the same functions for the calling classes.

Now if you make a variable in an interface, what would it bring, if the variable is not public? the interface is there to generalizes the communication with other classes and if the variable or the function is not public it can not be used from other classes. i hop this will help you.

chris
  • 125
  • 13
-1

Interfaces where designed to be a means of communication between objects. This is why all methods have to be public. It's merely a design choice.

EDIT: As suggested by @jwenting, it's better to say that this is a core architecture choice instead of a design choice.

Aurasphere
  • 3,841
  • 12
  • 44
  • 71