Sunday, August 06, 2006

Difference between inheritance and polymorphism

Inheritance is the mechanism which allow to generalize / specialize classes into hierarchy, the base class being the most generic, the classes inheriting from this base class adding some specialization:


Vehicule

ground vehicule

car

truck

bicycle


Polymorphism is the ability to use an object without knowing exactly is type, just by using the base class as a type and being able to call method (virtual method) for it.


Having a virtual method StartEngine() on Vehicule, and having this method redefined for all the other class will allow to call that method on a pointer on Vehicule, and the corresponding method in the correct class will be called thanks to polymorphism.

No comments:

Post a Comment