[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Beginning Java
 
RSS feed
 
New topic
Author

Why did I get this Outout?!

Abder-Rahman Ali
Ranch Hand

Joined: May 22, 2008
Messages: 133

In this code I was expecting " a d a" and the result is "a a a". Why is that?



Thanks.

This message was edited 2 times. Last update was at by Abder-Rahman Ali

Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Messages: 58

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

Joined: Aug 05, 2005
Messages: 1708

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

This message was edited 1 time. Last update was at by Joanne Neal


Joanne
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Messages: 58

I see.. thanks
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Beginning Java
 
RSS feed
 
New topic