Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 what's the point of polymorphism?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
raptors892004




PostPosted: 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 Razz).
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Dec 09, 2006 10:11 pm   Post subject: (No subject)

code:
class Person {
   void speak() {
      System.out.println("Hello!");
   }
}

class AnnoyingPerson extends Person {
   void speak() {
      System.out.println("HOWDY!");
   }

   void annoy() {
      System.out.println("Poke...");
   }
}

class UtilityClass {
   void speak(Person p) {
      p.speak();
   }
}
raptors892004




PostPosted: Sat Dec 09, 2006 11:03 pm   Post subject: (No subject)

code:
Person poly = new AnnoyingPerson();
poly.annoy();


Doesn't annoy Sad

But if I go,

code:
AnnoyingPerson raptors = (AnnoyingPerson)poly;
raptors.annoy();


Thank god for downcasting. My question is why go through all that? Isn't it much simpler to just go:

code:
AnnoyingPerson raptors = new AnnoyingPerson();
raptors.annoy();


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 Sad ). 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.
wtd




PostPosted: Sat Dec 09, 2006 11:06 pm   Post subject: (No subject)

Look at the Utility class. What kinds of objects can it accept as arguments?
Tony




PostPosted: Sat Dec 09, 2006 11:21 pm   Post subject: (No 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?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
raptors892004




PostPosted: Sun Dec 10, 2006 12:52 am   Post subject: (No 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() ?
wtd




PostPosted: Sun Dec 10, 2006 1:12 am   Post subject: (No 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.
gsquare567




PostPosted: Sun Dec 10, 2006 2:53 pm   Post subject: (No 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'.
Sponsor
Sponsor
Sponsor
sponsor
raptors892004




PostPosted: Sun Dec 10, 2006 5:02 pm   Post subject: (No 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 Smile
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: