File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why did I get this Outout?!

 
Abder-Rahman Ali
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this code I was expecting " a d a" and the result is "a a a". Why is that?



Thanks.
 
Kalabaw moo
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the superclass's method "doStuff()" is not overriden. If you wanted to override the superclass's method then you have to declare it abstract. Try to read more about inheritance.
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kalabaw moo wrote:Because the superclass's method "doStuff()" is not overriden. If you wanted to override the superclass's method then you have to declare it abstract. Try to read more about inheritance.


Non-abstract methods can be overridden too. The reason doStuff is not overridden is that it is static. Static methods cannot be overridden.

Having said that though, that is not the reason for the output. Static method calls are resolved at compile time and even though you are calling the method on an instance of the class, the compiler ignores this and just uses the class type to decide what method to call. Because this is done at compile time and 'a' is an Animal array, your code is actually just the equivalent of
 
Kalabaw moo
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see.. thanks
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic