Hi there, this is my first entry in two years. Stay tuned, you will find some cool stuff.
As a warm up, something is kinda making me confused. We all know how sophisticated language java is. But I came across something which I believe is a design error.
If I have 2 separate classes in two different packages
package in;
public class Animal {
public Animal() {}//accessible only throw inheritance or throw the dot operator if the accessing class is in the same package
protected void eat(){System.out.println("animal");}}
--------------------------------------------------
package out;
public class Horse extends in.Animal {public Horse() {}/** Override the protected method eat*/
protected void eat(){System.out.println("horse");}}
-------------------------------------
package in;
import out.Horse;
public class BarnHorse{public BarnHorse()
{Animal a=new Horse();//calling polymorphiclly the eat method in the horse class a.eat();}}
the code compiles fine and calls the Horse version of eat and outputs "horse" although the calling class shouldn't know anything about the Horse eat version.
please share your opinion
No comments:
Post a Comment