简体   繁体   中英

Why are interface variables public

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM