-1

Possible Duplicate:
Try to describe polymorphism as easy as you can

I have trouble understanding what exactly polymorphism is? And for example how would this code use polymorphism

I have a car class and a method inside that called countprice and i also have a jeep class that inherits from the car class.

List<Car> carlist = new List<Car>();
Jeep jeepABC123 = new Jeep(800, 14, 20, 10, 3500);
carlist.Add(jeepABC123);
Sedan sedanGHA321 = new Sedan(600, 7, 20, 10);
carlist.Add(sedanGHA321)

// This code
foreach (Bil bil in carlist)
{
revenue = revenue + car.countprice();
}
Community
  • 1
  • 1
Benny
  • 35
  • 7
  • Yes, this is a dup. Read the post linked to by Alastair and if there is still something you don't understand then reformulate your question. – Todd Dec 04 '12 at 05:12

1 Answers1

0

It's simple, you can put Jeeps into your array of Cars

Maybe you can also implement Truck which would inherit from Car, then your carlist can have both Jeeps and Trucks in it at the same time.

Polymorphism means that you can put any derived class into a variable declared for the base class, and it's a very fundamental concept of object-oriented programming.


I first truly appreciated polymorphism when I started writing simple games.

for instance:

foreach(Enemy enemy in Enemies)
{
    enemy.update();
}

each of those update calls can do something different, one can cause the enemy to shoot at you, where another can cause the enemy to change it's position, but they all have an update call, and all the update calls have the same signature.