Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about interface

 
sunanda kadam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why interface methods are not static ?
though fields can be static .......
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sunanda,

method declarations of an interface are public abstract by default because there is no implementation and they have to be implemented by any concrete class which implements the interface. And abstract methods can't be static because they don't have an implementation which could be called

Marco
 
sunanda kadam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still not cleared can you please elaborate more
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are allowed to assign a value to a field in an interface declaration so you can later use this field as a static constant.

But you can't give an implementation for a method in an interface declaration because methods in an interface are abstract by default: abstract = NO implementation!!! So it's obvious that such a method can't be static at the same time because you are allowed to call static methods but you are not allowed to call abstract methods!

Anyway the main reason for interfaces is to define abstract method prototypes which have to be implemented by any concrete class to fulfill the contract of the public API of the interface. So it doesn't make sense to define method implementations in an interface.

Now better?

Marco
 
sunanda kadam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marco
Now its cleared.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic