This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Looking at your code, you have actually overloaded(not overridden) the method "play" across base & derived classes. At line 1 you have a reference of type "Game" and you are attempting to invoke a method play(String s) (which is actually not part of the interface exposed by the "Game") with the same reference.
Even though the reference denotes an object of type "Soccer" at runtime the compiler would not know that and hence flags it as compile-time Error.
Your first line (line 1), you are invoking with the reference of type Game, but the actual object is type soccer. But there is no method with the String argument. The invoking of the method may be overridden or overload, that is another story. (For overridden method, the method is invoked on the actual object type basis, polymorphism works here, virtual method invocation etc... for overloaded method, the method is invoked reverence type only)
For your line 2, is it not working?
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Because Game class doe not have play method which accepts String argument, so the code fails.
Even if yo are referring to the child class using parent class object you can't directly call the child class method until you do not have the same method in parent class (overloaded methods , which perform polymorphism). In your case you are not actually performing the overriding but you are performing overloading and in case of overloading the actual method of the object is called.