|
Post by SquirrelHunterPro on Dec 6, 2001 22:24:13 GMT -5
sucks.... can anyone sum it up in a sentence or a paragraph on what it is and how to work with it?
|
|
|
Post by kermit on Dec 7, 2001 22:01:15 GMT -5
the Nino and Hosch text actually defines it on pg. 342: "polymorphism: dynamic behaviour by which the method performed as the result of a call to a given object is determined at run-time by the class of the object."
this question actually came up in lecture today, too.... putting the text def'n and from what i understood in the lecture together, i think it means different methods with the same name from different classes. so for example, from a8, where there was more than one p1(), (one from class X and one from class Y), it is decided at runtime, which one of those methods to use, depending on what class the object is from.
does that make sense?
|
|
|
Post by bladehunter on Dec 9, 2001 9:09:07 GMT -5
It's sort of like that.
From my past lessons from high school, polymorphism means same type of object but are totally different in functionality.
One real-life example are radios. If I have two radios, they are of same type. But one radio may be different than the other, let's say, in the types of frequencies they use or their colours.
Hope this helps in explaining.
|
|
|
Post by SquirrelHunterPro on Dec 10, 2001 20:16:12 GMT -5
dudes...no offence but I know what it means, I just want to know how to use them correctly.... i.e. extends classA I know that a subclass does not inherit constructors, but they inherit other stuff.... and that if a call is made to a method not present in the current class, it will look at the super class and then the super super class etc.... hmm.... ok so I know how to use it, but now how do I cast? casting sucks... it's not like Im a necromancer and can case bone spirit or bone spear..... poison nova is actually quite effective......
|
|
|
Post by Majin_Blues on Dec 10, 2001 20:41:23 GMT -5
I know that a subclass does not inherit constructors, but they inherit other stuff.... and that if a call is made to a method not present in the current class, it will look at the super class and then the super super class etc.... hmm.... ok so I know how to use it, but now how do I cast? casting sucks... it's not like Im a necromancer and can case bone spirit or bone spear..... poison nova is actually quite effective...... note a subclass does not inherit private methods. As for the automatic method referrals to the super, don't you have to add in "super" before the method? i.e. super.doIt()? For casting, it's the same as normal casting with ints and chars and stuff: given object A, object B extending A, object C extending B A a = new A; B b = new B; C c = new C a = b; (not sure about this... is this do-able?) c = (C)b; (kinda sure this cast works)
|
|
|
Post by Brutal_Chicken on Dec 11, 2001 12:59:13 GMT -5
a = b; is doable iff A is the superclass of B which it is in your example. A car can be a BMW but not every car is a BMW so you can't do the converse, b = a;
It compiles but since b would theoretically have more methods than a then there's a run time error.
|
|
|
Post by Majin_Blues on Dec 11, 2001 21:36:39 GMT -5
woo hoo... i remembered something! ;D
just to double check, is the cast method above valid? (er... compile-able and run-able?)
|
|
|
Post by Evotamer on Dec 12, 2001 3:33:39 GMT -5
Ahh the crap I hate... Suppose you got 3 classes A B and C class A { blah {...code...} blearg{...code...} blag{...code...} } class B extends class A { blah {...code...} stuff {...code...} more stuff{...code...} } class C extends B { blah {...code...} stuff {...code...} doThis{...code...} } and in your main you have something like A a = new A; B b = new B; C c = new C; now suppose you call something like this: c.stuff This is gonna be running 'stuff' from class C. Why? Well because class C is the class you declared it. Now suppose you have this: c.blearg Now this is gonna be calling 'blearg' from class A. Why? Well because C doesn't have the blearg method. So now Java searches class B... DOH... It doesn't have it either... so now it checks class A for it and boom it's there.. so it calls it. The way Java searches for methods is that it looks in the class that the object was located in, in this case C. Then if the method is not there, it checks the parent ('super') class of it, in this case B. Now if this fails... it searches A for it. If A which is the top class in this hierachy, then it gives you a compiler error Now here's Casting at work: (B)c.blah This calls B's blearg this time... just from the cast you convert C into a B object and it calls B's blearg instead. (I'm not 100% sure on this just seeing what I remember). Normally, and in most cases I try not to 'double' up on the method names... If you can declare an 'abstract data type' or module then just create a backbone structure do that. One thing I hate is casting my variables and methods... it adds confusion and crap like that.. ... And when you learn to program in C, you'll learn that casting crap all over can lead to bugs... Heck.. I spent 4 hours debugging a program that DID work correct just because I used int instead of a double for 1 dumb variable *sigh*. Heh... Hope this helps... Evotamer
|
|
|
Post by Brutal_Chicken on Dec 12, 2001 12:50:18 GMT -5
Riddle me this;
class Chimp { sleep method }
class Zebra extends Chimp { sleep and eat methods }
class Lion extends Chimp { sleep and eat methods }
MAIN
1 Chimp a = new Chimp(); 2 Zebra b = (Zebra) a; 3 a = new Zebra; 4 a.eat(); 5 a.sleep();
6 Lion c = new Lion(); 7 c.eat(); 8 Zebra d = (Zebra) c; 9 a = (Chimp) (new Lion()); 10 c = (Lion) (new Chimp());
Which of these cause COMPILER errors?
|
|
|
Post by Evotamer on Dec 12, 2001 13:41:25 GMT -5
3 a = new Zebra; Should be a = new Zebra(); 8 Zebra d = (Zebra) c; I'm guessing for this, but c is a instance of a lion, and zebra doesn't extend the lion thing... it extends chimp which makes this illegal.
|
|
|
Post by Brutal_Chicken on Dec 12, 2001 14:58:54 GMT -5
Umm 3 was a typo... my bad but I had the same qualms about 8. Lion and Zebra are sibling classes so I'm unsure of how casting applies to this case.
|
|
|
Post by Evotamer on Dec 12, 2001 17:34:53 GMT -5
From my understanding.... your hierarchy is like this
Chimp / \ Zebra Lion
The only think that you can cast is if the sub / superare extended off one another. Zebra and Lion will never be casted to one another since they don't share anything in common, relatively speaking.
Much like if you have a hierachy of thing / \ plant animal / / \ flower dog cat A flower is a thing, and so is a dog... but a flower is still not a dog nor does it share any distinct properties that distinguish one from another. So you can't cast flowers with animals... but you can cast a dog with an animal.
|
|
|
Post by Brutal_Chicken on Dec 12, 2001 17:36:35 GMT -5
So you're saying there'll be a compiler error because they're siblings? Makes sense, thanks!
|
|
|
Post by Evotamer on Dec 12, 2001 17:51:11 GMT -5
Basically what I mean, is that if the two objects / things that you have are nodes in a different branch, then you can't cast them because they don't inherit the properties of the other.
i.e. A Dog needs to eat and drink while a flower or plant just needs water. Or a dog may have a method saying it can change locations, while a flower doesn't change locations unless uprooted or caught in a very strong powerful wind / tornadoe (uhhh too much reality).
|
|
|
Post by Majin_Blues on Dec 12, 2001 19:36:12 GMT -5
thing / \ plant animal / / \ flower dog cat all those other things you talked about, ok... just to double check, using the diagram above, you can cast a cat as a thing, right? i.e. cat a = new cat(); thing b = new thing(); b = a; a = (cat)b; (are the last two lines right?)
|
|