Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why cannot interface methods be static

 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
Can anyone explain why cannot interface methods be static ?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
ok thank you i got it .
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are Welcome!
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use static keyword with abstract only and only in one case i.e. in inner classes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic