static implemetation of interface method

T

Timo Nentwig

Hi!

Why actually can a interface method implementation not be static?

public interface IF
{
public /* static */ void method();
}

public class Test implements IF
{
public /* static */ void method()
{
}
}
 
L

Lee Fesperman

Timo said:
Hi!

Why actually can a interface method implementation not be static?

I'll just give you the simple explanation. An interface can only be used two ways:

+ As something another interface extends or a class implements.

+ As an interface reference.

Ignoring Java's kludge in that area, a reference can only actually be used to refer to
instance methods and fields. In addition, having interface static methods would conflict
with the Java rules for static members. What would <inteface-name>.staticMethod() mean?
Where would it find the implementation? What about multiple inheritance of interfaces?

IOW, it would cause big problems, not unlike the proposal for specifying constructor
signatures in an interface. That's some of the reasons why Java doesn't support it.
 
C

Chris Uppal

Timo said:
Why actually can a interface method implementation not be static?

What /exactly/ are you wanting to be able to do ?

An interface defines (part of) a contract that an object will meet by having
implementations of the various methods named by the interface. So what would a
static method mean ? It couldn't be part of the implementation of the
interface, because the static method (by definition) is not attached to any
object.

-- chris
 
D

Darryl Pierce

Timo said:
Why actually can a interface method implementation not be static?

public interface IF
{
public /* static */ void method();
}

public class Test implements IF
{
public /* static */ void method()
{
}
}

Because static methods cannot be overridden by child classes, they're
attached to the defining class and not to any instances.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top