File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes static Methods @ Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "static Methods @ Interface" Watch "static Methods @ Interface" New topic
Author

static Methods @ Interface

Mohammed Yousuff
Ranch Hand

Joined: Oct 17, 2007
Posts: 198
Why we can't have a static methods in a interface... i was thinking this will make sense right ??

because what ever the variable we declare in the interface are static then why can't methods ???


My Thoughts : http://passion4java.blogspot.com
Try not to become a man of success but rather to become a man of value.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Mohammed,
The methods in the interface are meant to be "inherited" by the class implementing the interface. If they are static, this does not occur.

If you have static methods, they really should live somewhere else.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Mohammed Yousuff
Ranch Hand

Joined: Oct 17, 2007
Posts: 198
Hi Jeanne,

Yes i do agree with your comments. Because the static methods will live in the class objects not in the instance object....

However can't we implement polymorphisms at Class level ???...

In the above code when i tried cast a child class to parent class it gives a error ... but the casting a String class to Object works with out any errors


As per my understanding the polymorphisms and inheritance are allowed only upto the instance level...So we can't do this in the Class Level...


do you have any thoughts why they are not implementing this functionality on the class level
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
Child.class and String.class return a Class object
You can cast Class to Object, but you can't cast Class to ParentClass.

And, static methods are not inherited. The sleep() method in ChildClass is not overriding the sleep() method in ParentClass. Make them not static.
[ April 21, 2008: Message edited by: R Lopes ]
jaspreet atwal
Ranch Hand

Joined: Sep 05, 2007
Posts: 52
Hi,

I do not understand what is happening in following line:



what is ChildClass.class?


Still Learing..
Abdul Kader
Ranch Hand

Joined: Apr 11, 2007
Posts: 115
So inheritance works only with the instance object level not for the Class Objects (child.class) ..

Is that right ??
Victor Emanuel D. Basso
Greenhorn

Joined: Sep 03, 2008
Posts: 1
Rodrigo Lopes,
And, static methods are not inherited. The sleep() method in ChildClass is not overriding the sleep() method in ParentClass. Make them not static. -


actualy, static methods ARE inherited and overridden.

I just tried this:

and it resulted in:

metodo de A
metodo de A



Please correct me if I'm wrong.
[ September 03, 2008: Message edited by: Victor Emanuel D. Basso ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Welcome to JavaRanch, Victor Emmanuel!

I am afraid you are mistaken; static members are neither inherited (according to the Java Language Specification) nor overridden. The metodo method reappears in "B" because "B extends A" means a B IS-A(n) A.
Try this in Java5 or Java6:You ought to get a compiler error.

Static members (and instance fields) can be hidden not overridden (we have an FAQ about that).
Please do a search of JavaRanch; there have been several threads on very similar subjects very recently.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
[Campbell]: I am afraid you are mistaken; static members are neither inherited (according to the Java Language Specification) nor overridden.

I certainly agree that static methods are not overridden. But according to the JLS definition, they are inherited:

The members of a class type (�8.2) are classes (�8.5, �9.5), interfaces (�8.5, �9.5), fields (�8.3, �9.3, �10.7), and methods (�8.4, �9.4). Members are either declared in the type, or inherited because they are accessible members of a superclass or superinterface which are neither private nor hidden nor overridden (�8.4.8).


Nothing in that definition prevents statics from being considered inherited. Yes, statics behave differently than nonstatics when it comes to overriding vs. overloading. But that doesn't mean they aren't inherited.

[Campbell]: Try this in Java5 or Java6:

As I recall, @Override is not enforced in JDK 5. Unless they added that in a later bugfix release, but I doubt it.

Also, note that the rules for @Override are not exactly the same as the JLS definition of overriding. For example, JLS 9.4.1 makes clear that an interface method may override a method declared in a superinterface. But if you try using @Override in this case, it's not allowed. So we can't assume that @Override matches the JLS definitions.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
I was obviously mistaken about inheritance. Sorry.
@Override was introduced in Java5.
And we are not working in an interface, but in a concrete class, where putting @Override before a static method ought to produce a compiler error.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
[Campbell]: @Override was introduced in Java5.

Introduced, yes. But not enforced. It was ignored by the compiler, whether or not it was on a method that actually overrode anything (by any definition). If you want Victor to observe a compiler error for the example given, he probably needs to use JDK 6, not 5.

[Campbell]: And we are not working in an interface, but in a concrete class, where putting @Override before a static method ought to produce a compiler error.

I'm aware of that. My general point was that @Override does not map directly to "overriding" as defined in the JLS. The folks who defined the @Override annotation were apparently on crack or something. Whether or not @Override compiles in a given program has little bearing on whether it's really overriding (in the JLS sense) or not. To put it another way, I agree with your conclusion (static methods can't be overridden) but not with your reasoning (because the @Override meaning is different from the JLS meaning of override). Yeah, it's nice that @Override seems to support your (correct) conclusion, but that's just coincidence as far as I'm concerned. The @Override annotation is not to be trusted as a definition of what "override" means.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Check out Overriding vs Hiding.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
Thank you, Mike; I shall have to find Java5 somewhere and try that annotation out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static Methods @ Interface