File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why... interface methods can't be static?

 
Ramakrishna Nalla
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
hatim osman
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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....
 
Barend Garvelink
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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..
 
Wensheng Li
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • 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();
}
 
Pui Shing Lee
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Kanchan K Bulbule
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Joyce Lee
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnks Joyce.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic