Nikki Smith wrote:
Last thing, is I'm supposed to use an example of polymorphism, but I can't even understand what it is, muchless what it looks like. Could someone please help? :3
Polymorphism is simply the concept that when you call a method on an object, you know that something will be done, but there's no guarantee *how* it will be done. Sound confusing? It isn't, really. Let's consider a concrete example from nature.
Mammals reproduce by giving birth to live young. So, if we were to describe that in a class, you might have something like this:
Note that I didn't bother to define the superclass Animal of the method giveBirthToLiveYoung; this example is not intended to be code complete!
Now, for a fun fact about nature: not all mammals give birth to live young. (You may or may not already know this). There is a group of mammals called monotremes that actually lay eggs. (They are still mammals because the mothers produce milk for the young once they have hatched.) The Platypus and the Echidna are the members of this group. We may describe this behaviour in a class this way:
Now, we have a subclass of Mammal that lays eggs instead of giving birth when asked to reproduce. It can inherit other methods of Mammal (perhaps "produceMilk()"), but when it comes time to reproduce, it lays eggs.
So this method:
takes a Mammal as a parameter and tells it to reproduce. You know that it it going to reproduce *somehow*, but not exactly how (unless, of course, you know the exact class of the parameter-- but you don't, and in most cases where polymorphism comes into play, you really shouldn't care). That Mammal could be a live-birth mammal (like a Cat or a Dog) or it could be a Monotreme -- it could lays eggs.
And that's polymorphism in a nutshell. For a more detailed example (again, using animals), check out our campfire story
How My Dog Learned Polymorphism