Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why cannot interface methods be static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why cannot interface methods be static" Watch "Why cannot interface methods be static" New topic
Author

Why cannot interface methods be static

ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

Hi ,
Can anyone explain why cannot interface methods be static ?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

ragi singh wrote:Hi ,
Can anyone explain why cannot interface methods be static ?

What is the point with declaring them as static? Static methods can't be overridden and loaded at class loading time.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

Because all methods declared in an interface are (implicitly) also "abstract" methods, even through explicitly we are not mentioning it but all methods will have "abstract" modified attached to them.



It is not possible to have abstract static void eat(); method check below code
ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

then i think it should all the more work , ie in the following example:-

My question is why cannot the interface Brand declare the method getBrand() static , because if does we can provide a static method implementation in Car so that the method can be invoked from the class Car as Car.getBrand()
ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

because in the interface itself if we make the method static the method should be associated only with the class implementing it .
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

ragi singh wrote:then i think it should all the more work , ie in the following example:-
public interface Brand{

public abstract Brand getBrand();

}
public class Car implements Brand{

public Brand getBrand(){}

}
My question is why cannot the interface Brand declare the method getBrand() static , because if does we can provide a static method implementation in Car so that the method can be invoked from the class Car as Car.getBrand()


You are coming on that way. Then you'll break polymorphic invocation of methods! What will happen, when you've invoked the static method with a sub class object with the super class/interface reference? Even though no relation with objects and static methods!
ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

ok thank you i got it .
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

You are Welcome!
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

you can use static keyword with abstract only and only in one case i.e. in inner classes.


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why cannot interface methods be static