• Post Reply Bookmark Topic Watch Topic
  • New Topic

(Design Perspective)Static method in interface  RSS feed

 
shri Sonparote
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Is there any answer why there is no static method in interface
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16048
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In an interface you cannot provide an implementation of a method and static methods are not polymorphic (they cannot be overridden), so it would make no sense to be able to have a static method in an interface: there would be no way to provide an implementation for the method.
[ July 12, 2007: Message edited by: Jesper Young ]
 
shri Sonparote
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Jesper
But we can redefine static method in implementor class.
So in that way we are giving implementation.

Regards
Shrikant
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16048
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but static methods are not polymorphic.

Polymorphism is this: When you have a reference of an interface or superclass type, but the actual object that it refers to is of a class that implements the interface or a subclass of the superclass, and you call a method on the reference, then Java dynamically looks up the method in the actual class of the object. For example:

The context of a static method is the class, not the object that you call the method on.

It is possible in Java to call a static method via an object reference, but you should never do this, because this is confusing (in my opinion this is a flaw in the Java language; it should not have been possible to call a static method via an object reference).

If you do this, it appears as if you are calling the static method on an object, but you are really calling it on the class of the reference type.

Java regards the two sayHello() methods in the classes Super and Sub as two separate methods (that happen to have the same name). sayHello() in class Sub does not override sayHello() in class Super, as with non-static methods.
 
shri Sonparote
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Jesper

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