• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

are static methods inherited

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read on Jane's website that:
* main() is inherited and can be overridden if not declared as final
This does not make sense to me? because main needs to be declared as a class static method. When I think inheritance, I think polymorphic behaviour, so how can a static method be inherited and overridden? You are not allowed to use the 'this' or 'super' keyword inside of main.
[ May 18, 2003: Message edited by: Rajinder Yadav ]
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the static methods are inherited. But they are not overriden.
Instead they are hidden.
 
Rajinder Yadav
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from what the java compiler is telling me it seems static methods do get inherited and that one can override a static method :roll:

1. if you run the program below, you will see that static method are not polymorphic
2. in class foo, if you add final to the who method you will get an error, which seems to indicate static methods do get inherited
Compiler error
test.java:1: who() in foobar cannot override who() in foo; overridden method is
static final
public class test{ static public void main(String[] arg) {
test t = new test(); t.showWhoCalled(new foo()); t.showWh
oCalled(new foobar()); } void showWhoCalled(foo f) {
System.out.println(f.who()); }}class foo{ final static String who() { retu
rn "foo"; }}class foobar extends foo{ static String who() { return "foobar"; }
}
3. if you removed the keyword static from the who() method in class foo and foobar, you will see the polymorphic behaviour
(*) conclusion: class static methods to get inherited, can be overridden but are not polymorphic


[ May 19, 2003: Message edited by: Rajinder Yadav ]
 
pinky yadav
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
method binding for static methods happens at compile time as they are not overidden. (they are inherited)
therefore both times method from foo is getting called as we are calling the method using reference of type foo.
Hope this is helpful.
Pinky
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inheritance does not imply polymorphism (see C++ for examples). Overriding implies polymorphism. Static methods are inherited. They can not be not overridden, instead they are hidden. Static methods are not polymorphic.
reply
    Bookmark Topic Watch Topic
  • New Topic