This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Unexpected result through same method called differently  RSS feed

 
Greenhorn
Posts: 5
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am posting this for my accountability buddy at work, with whom I am trying to keep motivated to get through my OCA certification.
The following code has interesting output:



Why does the number of feet change for the last WildAnimal object named wildAnimalAndTiger? I can understand that the object is a WildAnimal, however, that would mean both the results would be 4?
Although I am not surprised by this result and "feel" like this answer makes sense somewhere, I cannot explain it fully why it is so. Does it have to do with the way getAmountOfFeet() is called? Is there a difference when it is done through the object directly, and when it is done through another method inside the same object?

It is probably out of scope for the OCA exam, but we are still very curious to get the full answer on this
 
Marshal
Posts: 64496
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to a more general forum where you will probably get more attention.

It is polymorphism at work. When you have two instance methods with the same name and parameter types in a superclass and a subclass, the subclass' method is said to override the superclass' method. That doesn't apply to static methods. in the case of line 5, the runtime type of the object is Tiger, not wild animal. That is how polymorphism works. The Tiger version of the instance method is called, and that calls the static method from the same class (8 feet).
This shows how much confusion you can get if you declare static methods with the same name in superclass and subclass; they don't override each other but hide each other: see this FAQ.
 
Bartender
Posts: 2287
95
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the correct way of writing your code:


Hope this clarifies your doubt. One should not use objects to refer to static content, it will cause confusion as shown in your case.
 
Get off me! Here, read this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!