Hi, Mayank
It works like this:
In Animal, eat() is public. public access means just that, eat() can be called from any portion of your
Java program that can execute a method.
the other three access levels restrict access as follows:
1) protected - method is accessible only to classes in the same package as the original class, or to classes that extend it.
2) default access - method is accessible only to other classes in the same package.
3) private - method is only accessible to other methods in the same class.
thus, Horse's eat() method is only accessible to other methods inside horse.
This means that even though your reference type is Animal, the implementing type is actually a Horse with a private eat method. This means that the code
violates the access control rights - because a is actually a Horse object, eat() is not accessible and thus the code is semantically incorrect and thus cannot be compiled.
If you somehow managed to get it to compile, I imagine that you'd get a NoSuchMethodException or some other nasty error at runtime.
This is why the Java language specification allows you to decrease the access control, since doing so will not result in the situation above.
Hope this helps.
Jeremy