This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

(Design Perspective)Static method in interface  RSS feed

 
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
 
Java Cowboy
Sheriff
Posts: 16084
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: 16084
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!