0
Exact meaning of polymorphism?
What if i write "Dog a = new Dog();" and "Cat b = new Cat();" ? Does this mean I'm creating an object 'a' which belongs to Dog class and similarly 'b' for Cat class?
3 Respostas
+ 7
Yes. It means you are creating an object of class Dog named 'a' and then allocating space for it by calling its constructor Dog(). Same goes for Cat.
Polymorphism means having same name but multiple form.
+ 4
polymorphism is being done when you've two classes "Dog" and "Cat" that both inherits from another class "Animal" for example. Then instead of doing : Dog a = new Dog(), or Cat b = new Cat();
you do : Animal a = new Dog(), or Animal b = new Cat();
both "a" and "b" will be considered as Animal objects but "a" will implements methods that are specific to Dog and and "b" will implements methods specific to Cat...
+ 1
Yes, it does. But this has nothing to do with polymorphism.
However, if your Cat() and Dog() inherits from another class, let's say Animal(), then we're talking about Polymorphism. If you have a method MakeNoice() in the class Animal() and then in Dog() you override MakeNoice(), then you have polymorphisized the shit out of the Dog().. Like this:
class Dog : Animal{
public override MakeNoice(){
Console.WriteLine("WOFF");
}
}
Polymorphism = multiple forms/shapeshifter or whatever.. It's like you can have the same base Class Object, because they are Animal() in the origin, but they have their own behaviour in detail because you override some features..
Look some more at the tutorial here at the site for this syntax.
EDIT:
Ben beat me with 2min :)
EDIT2: Check for syntax in the tutorials. My syntax may be wrong since it's C# syntax. But the polymorphism is the same in these two languages.