0

I've always been a little wary about polymorphism and still don't think I understand the concept to the fullest.

I understand polymorphism as an object can take many forms. So an object can be one form then be another?

Mammal (base class)
cat (subclass of mammal) 
supercat (subclass of cat)
Cat newCat = new Cat();

Now I want the cat to be a supercat, is this "Polymorphism"?

SuperCat supCat = (SuperCat)newCat;

Isn't this just like casting? When do you want to use casting? Is the above line of code valid? So newCat gets transformed to Supercat, does it give newCat more memory allocation? Then copies it into supCat?

NOTE -- Polymorphism is the use of interfaces?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
RoR
  • 15,934
  • 22
  • 71
  • 92
  • Sorry, your understanding of polymorphism is way off. Polymorphism is just a way for different types to share some behavior while differing in other behavior. If you want to be able to transform any `Cat` into a `SuperCat`, you'll have to write that code yourself. – mqp Jan 07 '11 at 06:00

5 Answers5

3

Just a comment on your note.

There are two types of polymorphism.

Compile time polymorphism:

This is where your polymorphic behavior is defined prior to compilation. This happens with method overloading e.g.

public bool DoSomeThing(int myVal)
{
  // some code here
} 

public bool DoSomeThing(double myVal)
{
  // some code here
} 

Runtime polymorphism:

This is where the polymorphic behavior happens at runtime. An example of this would be a method that accepts a type that implements a specific interface (or common base class). This interface could be of a different type depending on the code that is executing.

e.g.

public interface IWeapon
{
   void Strike();
}

public class Knife : IWeapon
{
   public void Strike()
   {
     Console.WriteLine("Stab");
   }
}

public class Gun : IWeapon
{
   public void Strike()
   {
     Console.WriteLine("Shoot");
   }
}

public class Warrior
{
  public void Attack(IWeapon weapon)
  {
    weapon.Strike();
  }
}

Now if the Warrior is given a Gun the output from Attack would be Shoot and for a Knife it would be Stab.

I haven't tested this code so there may be syntax errors etc., but you should be able to get the idea

Andrew
  • 5,215
  • 1
  • 23
  • 42
  • Thanks, before with interfaces I still always wondered why you'd use them besides to give classes the same behavior, but the idea of grouping them together, and using them in the fashion is cool indeed and makes sense. Thanks. – RoR Jan 07 '11 at 06:51
1

Polymorphism is where you interact with objects of a subtype through a base type interface. For example:

List<Cat> cats = new List<Cat>();
cats.Add(new Cat());
cats.Add(new SuperCat());

foreach (Cat cat in cats) 
{
    cat.Meow();  // Works whether cat refers to a Cat or SuperCat.
                 // If SuperCat has its own Meow implementation, that is called.
}
Jacob
  • 77,566
  • 24
  • 149
  • 228
1

Polymorphism allows one type to express some sort of contract, and for other types to implement that contract (whether through class inheritance or not) in different ways, each according to their own purpose. Code using that contract should not(*) have to care about which implementation is involved, only that the contract will be obeyed.

Taken from this post.

Polymorphism - Define In Just Two Sentences

Community
  • 1
  • 1
RoR
  • 15,934
  • 22
  • 71
  • 92
0

No this is not "Polymorphism" A SuperCat is a Cat, but not all Cats are SuperCat´s.

bratao
  • 1,980
  • 3
  • 21
  • 38
0

Polymorphism means that your SuperCat is a SuperCat, a Cat, and a Mammal, all at the same time. It can be treated as any of those three.

Poly -> Many

Morphism -> Mapping or 'type'

Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169