Tuesday, March 16, 2010

Polymorphism

In programming we deal with many kinds of user interface windows in the Microsoft Windows environment, such as message boxes that convey a message or a warning and windows for painting or writing. If you wrote, for example, a method called DrawWindow, we can associate this method with various objects that represent different kinds of windows. Based on the object associated with the method, the result would be different. Consider these examples:
    myButton.DrawWindow();        //draws a button
    myMsg.DrawWindow();        //draws a message box
    myPaintSurface.DrawWindow();    //draws a painting window
The three objects – myButton, myMsg, and myPaintSurface – are not instances of the same class, but instances of other classes derived from the base class that contains the original DrawWindow method. Each object is used to invoke a new definition of the method DrawWindow.

No comments:

Post a Comment