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
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.