This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enthuware question about polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enthuware question about polymorphism" Watch "Enthuware question about polymorphism" New topic
Author

Enthuware question about polymorphism

Daniel Martins
Ranch Hand

Joined: Oct 22, 2009
Posts: 45


Why is this not valid?

g.play("cosco"); at //1 and s.play("cosco"); at //2

S is a reference from Soccer but points to a Soccer object just before line 2...how come it cannot call the Soccer play(string s) method?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Isn't the problem at //1, not at //2 ? Did you try to compile it ?


[My Blog]
All roads lead to JavaRanch
Mandar B Kulkarni
Greenhorn

Joined: Feb 18, 2010
Posts: 2
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.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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%|
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

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.
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

@ Harpreet Singh janda
You are correct
I think instead of saying this
Harpreet Singh janda wrote:(overloaded methods , which perform polymorphism)

you want to say this
(overrided methods , which perform polymorphism).


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

Yeah, You are correct "Ninad Kulkarni"

Thanks for pointing the mistake
Daniel Martins
Ranch Hand

Joined: Oct 22, 2009
Posts: 45
Thank you for the help.

I do know the difference between overloading and overriding, however I completely missed it in this one - I know its obvious.

Cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enthuware question about polymorphism