Why interface variable or field more precise are not default?

295 Views Asked by At

I mean to say that Why cannot I have it default access specifier and please don't tell me that java developer made it like that I know that this link it give me that information ..My real Question why it cannot be default access specifier? Not having it private make sense as then variable will be useless ..But Why not default access specifier?

3

There are 3 best solutions below

0
On

The important thing is how public the interface itself is. It doesn't make sense to have the interface methods less public than the interface itself. So making interface methods public means they are visible whenever the interface is.

Access modifiers are there to protect implementation details, but if you wanted to hide some method you wouldn't expose it in the interface.

0
On

Firstly all values are public so that it can be accessed from anywhere.
Second thing,interface variables are static because Java interfaces cannot be instantiated in their own right; the value of the variable must be assigned in a static context in which no instance exists.
and 3rd the final modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned by program code.

Hope this will answer your question.

0
On

You should keep in mind that main task of interface is to provide a contract.

That contract is base for any other future operations. So it must be equal for everything.

What you can do is to declare a public, protected, private or (defalut) interface. But as far it goes for his members (methods, fields) the must be public.

The reason why they must, is that if some member would be available for restricted area it would deny main principle that every one should support contract on equals terms.

Would lead to redefinition of concept of interface as we know.


In case you need such thing you should more focus on the architecture of your system.