Polymorphism

Polymorphism

The term polymorphism comes from the Greek language and literally means "many forms." In the context of object-oriented programming, it means "do the right thing." For example, many classes can provide the same property or method, and a caller doesn't have to know what class an object belongs to before calling the property or method. A programmer might call Form1.Size or Text1.Size and know that the object will do what's right to resize itself.

Most object-oriented programming systems provide polymorphism through inheritance. For example, you might have two forms that inherit from the Windows.Forms.Form class. Each form could override the class's Close method. When the Close method is invoked for Form1, it might ensure that all database connections are closed. The Close method of Form2 might display a message box. The user simply closes each form, and then each form does the right thing. Not too terribly difficult, eh?

While you can implement polymorphism through inheritance, you can also use it through a programming interface that you can write. With polymorphism you can create hierarchies of classes and then treat the objects in the hierarchy as either similar or different depending on your needs. As we delve more deeply into classes later in the book, I'll examine this concept in more detail. The endgame, however, is the same. A program calls the same method on a different object and the right thing happens. Polymorphism is another large word for a straightforward concept.

The Three Pillars of Object-Oriented Programming: As Easy as PIE

Object-oriented programming is as easy as PIE. That's Polymorphism, Inheritance, and Encapsulation. We just covered the three cornerstones of all object-oriented programming.

  • Polymorphism.  Overriding methods of the same name for different objects to perform different actions.

  • Inheritance.  Deriving an object from a base class to inherit its properties and methods.

  • Encapsulation.  Hiding data within a class so that it can be manipulated only by subroutines or functions within the class itself.



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net