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();
}
It’s simple, you can put
Jeeps into your array ofCarsMaybe you can also implement
Truckwhich would inherit fromCar, then yourcarlistcan have bothJeeps andTrucks 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:
each of those
updatecalls 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.