This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why did I get this Outout?! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why did I get this Outout?!" Watch "Why did I get this Outout?!" New topic
Author

Why did I get this Outout?!

Abder-Rahman Ali
Ranch Hand

Joined: May 22, 2008
Posts: 138
In this code I was expecting " a d a" and the result is "a a a". Why is that?



Thanks.
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
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
Posts: 3419
    
  12
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


Joanne
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
I see.. thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why did I get this Outout?!
 
Similar Threads
Chapter II - Page 151 - Static doubt
static method can't be overridden but this code is not?
confuse for static method
Static method : Redefinition or an override ?
for static