• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Methods and Inheritance  RSS feed

 
Sondra Colagrossi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created two classes Super and Sub.
I put a int getX() in super and called it from Sub. Worked fine, but I was troubled because I thought static methods were not inherited.
I put a public static void main( String[] args ) in super and tried to run Sub. Viola NoClassDefFoundError.
Am I crazy or is this weird? :roll:
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods do not participate in polymorphism. They are inherited.
The error that you experienced has nothing to do with inheritance. Your JVM couldn't find some class definition. Perhaps you've a problem with your classpath or you didn't compile a necessary class.
Running java Foo or java FooSub with the following class definitions both display "Foo" to the console.
 
Sondra Colagrossi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response. My code is as follows. I'm running this from an Eclipse 2.0 IDE. I'm not sure where it gets its compiler or JVM from. The package spec is a wee bit different too.
Your explanation of polymorphism vs inheritance is illuminating too.
main in sub worked fine.

/* other class */


[ February 17, 2003: Message edited by: Bob Fred ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!