Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

why the fields in interface are always static and final?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could any one help me in understanding why fields in interface are always static and final? Also why interface methods cannot be defined as static or final?

What i feel the interface methods cannot be final because interface methods can be overridden differently in different class... please correct me if I am wrong....
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gagan Kinra wrote:
Could any one help me in understanding why fields in interface are always static and final


This I think is because interfaces dont contain any state information/have a state associated with them.

Gagan Kinra wrote:
Also why interface methods cannot be defined as static or final?


Interface methods are to be overridden, hence not final and static, except when a interface is being extend by another interface or an interface being implemented by a class declared as abstract.


 
Marshal
Posts: 74634
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

The answer is that back in 1994 or some similar time, somebody thought it was a good idea to store constants in interfaces. More recent thinking is that constants in interfaces are not always a good idea at all, however.
If you want interface methods to be final, and their body is written as ; how are they going to work?
The name interface means it is the public interface of an object, so interface methods are not static.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gagan Kinra wrote:Also why interface methods cannot be defined as static or final? What i feel the interface methods cannot be final because interface methods can be overridden differently in different class... please correct me if I am wrong....


At the risk of repetition, the main reason they can't be final is that they are, by definition, abstract. And that means that not only can they be overridden, they must be.

Winston
 
Gagan Kinra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also why interface methods cannot be defined as static or final?


I am able to know the reason for this.... Interface method cannot be final..because these methods are meant to be overridden but why Not Static? as per my understanding... they serve the same purpose ..I mean why both of these are required...only final should have served the purpose in case of methods.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gagan Kinra wrote:Also why interface methods cannot be defined as static or final?

I am able to know the reason for this.... Interface method cannot be final..because these methods are meant to be overridden but why Not Static? as per my understanding... they serve the same purpose ..I mean why both of these are required...only final should have served the purpose in case of methods.



Winston Gutkowski wrote:And that means that not only can they be overridden, they must be.


@Gagan: Bearing in mind this imperative: can static methods be overridden?

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gagan Kinra wrote:Also why interface methods cannot be defined as static or final?


I am able to know the reason for this.... Interface method cannot be final..because these methods are meant to be overridden but why Not Static?



Because static methods cannot be overridden.

as per my understanding... they serve the same purpose ..I mean why both of these are required...only final should have served the purpose in case of methods.



I don't know what you're saying here, but final and static are totally different and totally unrelated to each other. However, as Winston points out, the reason interfaces exist in the first place is to define something where every method must be overridden (implemented).

Since neither final methods nor static methods can be overridden, every method that is going to be overridden must be !static && !final. If it is static, that fails. If it is final, that fails.
 
Campbell Ritchie
Marshal
Posts: 74634
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I said earlier, they are to form part of the public interface of the object. If they belong to the object, they can’t be static. And remember, they are part of the public interface, so they must be public, even if you miss out the word public.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gagan Kinra wrote:Also why interface methods cannot be defined as static or final?


You'd probably have to ask the designers for a definitive answer to that one, but my suspicion would be:
1. For consistency - interfaces contain no implementation code at all.
2. For simplicity - this is just a guess, but I suspect it's easier not to allow interfaces to implement any kind of method.

It's certainly arguable that it would have been nice to be able to define methods that are public, static AND final in interfaces; and it's possible that other OO languages do allow it, but Java doesn't and I'm afraid you just have to live with it.

However, there's nothing to stop you defining a nested class for the purpose, viz:
Winston
 
Gagan Kinra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody
 
Campbell Ritchie
Marshal
Posts: 74634
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic