The method eat() in class Animal is private and hence is not visible outside of the class(including any subclasses). The eat method of the subclass (Horse) doesn't override the superclass' method as it isn't inherited by the class.
It just has the same name which is what makes it confusing.
Also, the polymorphic behavior of the reference 'a' would come into picture only if the method was overridden. Changing the signature of the eat method in the superclass to anything but private would give you the desired result.
The way you have defined the method is neither overloading nor overriding.
you can remember the logic behind this behvior by understanding the bytecode generated.
for normal method call, the compiler generates an "invoke" instruction which resolves the target class type at runtime... however, when a private method is called the compiler generates an "invokespecial" instruction which doesnt try to resolve the target method based on the instance type.
Do you want ants? Because that's how you get ants. And a tiny ads:
a bit of art, as a gift, the permaculture playing cards