• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why cannot interface methods be static

 
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
Can anyone explain why cannot interface methods be static ?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ragi singh wrote:Hi ,
Can anyone explain why cannot interface methods be static ?


What is the point with declaring them as static? Static methods can't be overridden and loaded at class loading time.
 
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because all methods declared in an interface are (implicitly) also "abstract" methods, even through explicitly we are not mentioning it but all methods will have "abstract" modified attached to them.



It is not possible to have abstract static void eat(); method check below code
 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then i think it should all the more work , ie in the following example:-

My question is why cannot the interface Brand declare the method getBrand() static , because if does we can provide a static method implementation in Car so that the method can be invoked from the class Car as Car.getBrand()
 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because in the interface itself if we make the method static the method should be associated only with the class implementing it .
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ragi singh wrote:then i think it should all the more work , ie in the following example:-
public interface Brand{

public abstract Brand getBrand();

}
public class Car implements Brand{

public Brand getBrand(){}

}
My question is why cannot the interface Brand declare the method getBrand() static , because if does we can provide a static method implementation in Car so that the method can be invoked from the class Car as Car.getBrand()



You are coming on that way. Then you'll break polymorphic invocation of methods! What will happen, when you've invoked the static method with a sub class object with the super class/interface reference? Even though no relation with objects and static methods!
 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok thank you i got it .
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are Welcome!
 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can use static keyword with abstract only and only in one case i.e. in inner classes.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic