• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enthuware question about polymorphism

 
Daniel Martins
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't the problem at //1, not at //2 ? Did you try to compile it ?
 
Mandar B Kulkarni
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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).
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, You are correct "Ninad Kulkarni"

Thanks for pointing the mistake
 
Daniel Martins
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic