Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static Methods @ Interface

 
Mohammed Yousuff
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ???
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34401
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohammed Yousuff
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I do not understand what is happening in following line:



what is ChildClass.class?
 
Abdul Kader
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So inheritance works only with the instance object level not for the Class Objects (child.class) ..

Is that right ??
 
Victor Emanuel D. Basso
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Overriding vs Hiding.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Mike; I shall have to find Java5 somewhere and try that annotation out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic