Computer Science Canada what's the point of polymorphism? |
Author: | raptors892004 [ Sat Dec 09, 2006 9:55 pm ] |
Post subject: | what's the point of polymorphism? |
After reading info about it and experimenting i discovered that by using code such as: A a = new C(); where class C is a subclass of A, I can't use some of the methods class C has because they are not in class A. So then my question is what is the use? Couldn't it be easier to just declare C c = new C() where class C extends A and get it over with? Maybe I'm missing something concerning the programming in the general concept.. If you can explain what "programming in the general" means it would be greatly appreciated too. Thanks for any replies and I have looked at the other polymorphism topic before writing this one (I tried to get as much info out of it as possible ). |
Author: | wtd [ Sat Dec 09, 2006 10:11 pm ] | ||
Post subject: | |||
|
Author: | raptors892004 [ Sat Dec 09, 2006 11:03 pm ] | ||||||
Post subject: | |||||||
Doesn't annoy But if I go,
Thank god for downcasting. My question is why go through all that? Isn't it much simpler to just go:
I had nothing to read on what general programming is (because that was the first thing mentioned in the Polymorphism slideshow but no info on it in the slideshow unfortunately ). Can anyone provide info on that? I guess that is the reason why Polymorphism is used. Like in the other post about polymorphism say you create a RewardCard reference to a CreditCard object. Then you can only use the CreditCard methods and not the RewardCard ones. So what is the point of doing that? If you want to use the RewardCard methods then you have to use downcasting and then you can access them. Wouldn't it have been simpler to just create a RewardCard object and since RewardCard is a child of CreditCard you would use all the overridden methods and the ones that are unique to RewardCard as well. Thanks for reading this long post btw. I apologize for any ideas that do not make sense in advance, since I just started learning about polymorphism. |
Author: | wtd [ Sat Dec 09, 2006 11:06 pm ] |
Post subject: | |
Look at the Utility class. What kinds of objects can it accept as arguments? |
Author: | Tony [ Sat Dec 09, 2006 11:21 pm ] |
Post subject: | |
raptors892004 - you are missing the point Quote: Person poly = new AnnoyingPerson(); poly.annoy(); a Person may or may not be able to .annoy. The question here is - what can a Person do reguardless of the fact if he's Annoying or not, and should we even care to call that method? |
Author: | raptors892004 [ Sun Dec 10, 2006 12:52 am ] |
Post subject: | |
wtd wrote: Look at the Utility class. What kinds of objects can it accept as arguments?
I really didn't quite get what the Utility class was used for in your code. Could you explain it a bit? Because doesn't it do the same thing as say p.speak() ? |
Author: | wtd [ Sun Dec 10, 2006 1:12 am ] |
Post subject: | |
Yes, it's an overly simple example. The speak method in UtilityClass can take either a Person, or an AnnoyingPerson. This is polymorphism. It allows me to make refinements on classes like Person, and yet, still use those refined classes with existing code that accepts a Person object. |
Author: | gsquare567 [ Sun Dec 10, 2006 2:53 pm ] |
Post subject: | |
thats what polymorphism means, 'poly' is many, 'morphism' is taking on different shapes, so the same method in wtd's example can take on more that 1 'shape'. |
Author: | raptors892004 [ Sun Dec 10, 2006 5:02 pm ] |
Post subject: | |
Thanks for all the replies and help. Funny thing is I never noticed info like this in the 110 page slideshow I got for CS class. This cleared up a lot of questions. Thanks once again |