Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

static Methods @ Interface

 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???
 
author & internet detective
Posts: 41763
885
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I do not understand what is happening in following line:



what is ChildClass.class?
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So inheritance works only with the instance object level not for the Class Objects (child.class) ..

Is that right ??
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 78433
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4616
61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 78433
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4616
61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22755
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out Overriding vs Hiding.
 
Campbell Ritchie
Marshal
Posts: 78433
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Mike; I shall have to find Java5 somewhere and try that annotation out.
 
Do not set lab on fire. Or this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic