Share via


Visual Basic Concepts

Polymorphism

Polymorphism means that 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.

For example, a Flea class and a Tyrannosaur class might each have a Bite method. Polymorphism means that you can invoke Bite without knowing whether an object is a Flea or a Tyrannosaur — although you'll certainly know afterward.

The following topics describe Visual Basic's approach to polymorphism and how you can use it in your programs.

  • How Visual Basic Provides Polymorphism   Most object-oriented languages provide polymorphism via inheritance; Visual Basic uses the multiple interface approach of the Component Object Model (COM).

  • Creating and Implementing an Interface   An extended code example shows how to create an abstract Animal interface and implement it for Tyrannosaur and Flea classes.

  • Implementing Properties   The interfaces you implement can have properties as well as methods, although there are some differences in the way properties are implemented.

  • Time Out for a Brief Discussion of Objects and Interfaces   Clarifies the terms object and interface, introduces the concept of querying for an interface, and describes other sources of interfaces to implement.

  • The Many (Inter)Faces of Code Reuse   In addition to implementing abstract interfaces, you can reuse your code by implementing the interface of an ordinary class, and then selectively delegating to a hidden instance of the class.

For More Information   With the Professional and Enterprise editions of Visual Basic, Polymorphism becomes a powerful mechanism for evolving systems of software components. This is discussed in "General Principles of Component Design" in Creating ActiveX Components in the Component Tools Guide.