This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I'm a new programmer in JAVA. I immigrated from PHP to JAVA :-)
I have a question about Polymorphism.
I don't know what's wrong with my code! Why i can't access to my child class from the parent class by Polymorphism.
Declare method eat() in class Animal as well. In Java, a method (or field) must be present in the reference class. Although the actual methods are Dog and Cat instances, the compiler doesn't know - all it knows is that they are Animal objects. And in your example Animal does not have the eat() method, so the compiler won't allow you to call it.
Of course, if you don't want to provide an implementation (i.e. method body), you can make both the class and the method abstract:
Your answer is great! Because i know now what's the abstract classes exactly.
Thanks a lot
But i don't know why the writer of "The joy of JAVA" book isn't explained it :S
He just wrote you can access to the subclasses from the super class :S
Joined: May 10, 2009
Can you explain how can i passing an object to other method as a parameter and how can i use it in this method?