This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface variables  RSS feed

 
Nikhil Nagrani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why interface variables are public,static and final by default?

public is fine so that it can be used by the implementing classes anywhere.

But why static and final?


 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea behind interface variables is that they are only to be used as constants. Hence static and final.
 
Nikhil Nagrani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell

But why..that's what i am asking?
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces only describe behaviour, not state. Instance variables are part of an object's state. Hence they are not allowed in interfaces. Instead of giving compiler errors when you add a public field to an interface without explicitly making it static, the language creators thought that making them implicitly static was a better idea.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And instance variables don't participate in polymorphism so there is no reason not to make them static.
 
Tarun wadhwa
Greenhorn
Posts: 14
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces are 100% abstract class and we create an interface to have some special functionality in our class.

The methods are implicitly "public and abstract" so that the class implementing must override as per the requirement.Even your compiler checks for that.

And the interface methods are by default public static final, because their value should no be by any implementing class.
For example a constant in a Math class "pi" exist if its value has been changed may cause a Blunder in Math Standard principle that use that value.

I hope you get the things. I tried to made it simple for better understanding.

"The more you play with it, the more you will learn"
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tarun wadhwa wrote:And the interface methods are by default public static final


Did you mean interface variables there ?
 
Matt Moran
Greenhorn
Posts: 14
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why? Well, a lot of the "why" of java is down to arbitrary decisions taken by those who decide Java's rules. That said, you can subclass an abstract class that has instance variables that are mutable. Interfaces are supposed to be clean & free of any actual code, just a promise to implement certain methods. You might as easily ask why they can have constants on them.
 
budsy remo
Ranch Hand
Posts: 103
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a good reason is that because interfaces can't be instantiated and all of their methods are abstract .Had they(interfaces) been capable of being instantiated how would you change the state of those instance variables through methods which are how one should change the variables because otherwise it would break encapsulation.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point, Budsy Remo; I had never thought of that.

When you are designing a language, you have to make decisions. Remember all these decisions determine how the compiler will compile your code, so some of them may have to be restrictive.
 
Tarun wadhwa
Greenhorn
Posts: 14
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joanne Neal,

Nope the methods in an interface are by default "public abstract".

about this very well explained above by respected Budsy Remo.

Only variables in an interface are public static final.

public: so they can be accessible through out the application any where.

static: so that only one copy of that exist.

final: so that no one can change the value of it in the entire program.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tarun: I think you should look again at what Joanne posted, particularly the part where she quotes you. You said that "interface methods are by default public static final" To repeat, YOU SAID that INTERFACE METHODS are STATIC and FINAL. Do you see a problem here?
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tarun wadhwa wrote:Interfaces are 100% abstract class


Not really, one should mix two of them together.
 
Tarun wadhwa
Greenhorn
Posts: 14
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Mike Simmons,

I will keep in mind that such mistakes i will not repeat again in future.

thanks Joanne Neal for highlighting the main context that was my mistake.
And yes there i mean that for instance variables.

@Amit Ghorpade
In an abstract class you can keep both abstract and non-abstract methods, but in interfaces all methods are abstract by default

even if you write an interface like this:

interface Bounceable{

void setBouncHeight();
void getBouncHeight();
}

Compilers see it as:


interface Bounceable{

public abstract void setBounceHeight();
public abstract void getBounceHeight();
}

Once again thanks a lot for pointing my mistakes.
 
Nikhil Nagrani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ALL

Thanks guys for the reply.Here is the what i can conclude from the discussion:

Interface variables are:

final(constant) : declaring them as normal instance variables would make no sense as they can't be changed as we don't have the defination of methods in interfaces (where we generally change the value of the instance variable thus following encapsulation) and just the declaration.So we declare them as final.

Static:Now as they are final i.e constant why make various copy for the same..so declaring them static.

Public: So that these static final variables can be accessed everywhere.No point in keeping these variables as private,protected or rather default.

Cheers...
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tarun wadhwa wrote:
@Amit Ghorpade
In an abstract class you can keep both abstract and non-abstract methods, but in interfaces all methods are abstract by default



I agree but that does not make it an 100% abstract class.
Try extending from more than one 100% abstract class.
The point is, interface is usually a behavior and not something more functional entity like a class.
 
manasee patel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A member is marked static so that it will behave consistent regardless of the instance. Any how we can't instantiate interface. The variable of interface will be meaningful only when some class implements it. And that class can be instantiated. So why to make it static?

What if the classes implementing the interface want to change the value of interface variable? Or why should not they want? As in abstract class the subclass can change the values of variables declared in the abstract superclass. Why is it problem for interfaces?
 
manasee patel
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I found the answer. It's here.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!