• 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... interface methods can't be static?

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abstract class can have static methods...
while it is illegal for interfaces...Why?

What rules are opposing to having static methods in interfaces?

Please clarify me..with your replies..

Thanks
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi..
Because more the one class can implement the interface, and each of them can have it's own implementation which is not necessarily match those of others.Ex. if the MouseListener methodes were static, then you can have more than one listner throughout the application life time.
Hope this help
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An interface contains no implementations for its method declarations, the methods have to be provided by classes that implement the interface. The classes provide the implementation of the methods by overriding the methods specified in the interface. static methods cannot be overridden. So it is useless declaring a static method in an interface, because nothing can provide its implementation.
 
Ramakrishna Nalla
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you hatim and Barry...

You may misunderstood my question...

By Barry:

static methods cannot be overridden. So it is useless declaring a static method in an interface, because nothing can provide its implementation.



Is there any rule static Vs interface...just like rule abstract Vs final
OR
Java Developers just thinks that..there is no need of static methods in interface...and put that reststriction...interface methods can't have body..
and given a STRONG Defination for interface...


Is there any problem... if we have static methods too along with method declaration in interface... (of course if Java Developers allowed)..

Thanks in Advacne....Waiting....
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ramakrishna Nalla:
Thank you hatim and Barry...

You may misunderstood my question...


Actually, Ramakrishna, you misunderstood the answer.

Static methods are bound to the class itself, not to the objects made of that class. In other words, If a static method is defined in another class, it's a different method, not an override. Which brings us to what Barry said:

Static methods cannot be overridden. So it is useless declaring a static method in an interface, because nothing can provide its implementation.



If you'd put a static method in an interface, you'd define a method that can not have a method body. As Barry so eloquently put it: that's a useless method.
 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ramakrishna,

think about it for a second there...
Interface methods are meant to be implemented.

An interface defines a contract between itself and implementing classes, saying what they must implement and not how. But if you where allowed to have a static abstract method, what good would it be if its going to be part of the class and not any particular instance.

This would completly put polymorphism back in its box (and where would OO programing be without polymorphism?)

hth.
Marzo.
 
Ramakrishna Nalla
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So much Thanks Barend..for correcting me...and also Marzo Kaahn...
Once again thanks to Barry...

I just think like below code hence that question arises for me..


Thanks to ALL..for your valuable replies..
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, abstract class can have static methods only for those methods which are NOT abstract themselves.For example, it won't compile if you write:

abstract class A{
static abstract void method();
}
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, just read the posts of this thread, and I have one question
hope anyone can explain to me:
Is there any restrictions on method defition within interface?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey ,

Interface methods can have either default or public access.
As they will be implemented by other classes complier complains when
modifiers like static , final and protected are specified for them.

Interface can also contain field but they will be final and static implicitly.


Cheers
kanchan

Preparing for SCJP1.4
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kanchan

Interface methods can have either default or public access.

Every method in an interface is implicitly public and abstract. See JLS3 9.4 Abstract Method Declarations.

Interface can also contain field but they will be final and static implicitly.

As for the fields, they're implicitly public, static and final. See JLS3 9.3 Field (Constant) Declarations.

Joyce
[ July 26, 2005: Message edited by: Joyce Lee ]
 
Kanchan K Bulbule
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnks Joyce.
 
reply
    Bookmark Topic Watch Topic
  • New Topic