Polymorphism in VB

Polymorphism and Interfaces

Polymorphism is the idea that many classes can provide the same property or method, without having to worry what type of class it is. For example, both a Dog class and a Mosquito class would have a Eat method, but they would go about this in very different ways. However, with Polymorphism, you do not need to worry whether the class is a Dog or a Mosquito. Instead, you know that both these classes will have methods that are used in both, such as an Eat method, and a Sleep method. Visual Basic allows you to do this through Interfaces.

Interfaces provide a template for classes, which forces any classes that use the Interface to have the same properties and methods as the Interface. For example, an IAnimal interface (the I standing for Interface) would have Eat, Sleep, Rest and Go methods. Therefore, any class that uses this Interface, must also have Eat, Sleep, Rest and Go methods. This can be very useful when programming.

Imagine you have a variable called 'Animal'. When your program starts, the user can select what animal he/she wants, ie a Dog, Cat, Sheep or Mosquito. Then, the user can control the animal by pressing buttons telling it to move forward, eat, sleep etc.

Without Polymorphism, you would have to create different variables depending on what animal was selected, and have different procedures that needed to be called when the user clicks on a command, depending on the animal.

Using polymorphism, however, you could use different classes, say cDog, cCat etc. All of these classes would implement an IAnimal interface, which then means that when the user clicks on a command, your code would simply call the appropriate method (ie Eat), and know that no matter what type of animal it was, it would have an Eat method. Calling this method, however, would involve different actions depending on the type of animal... Dog = Bark at owner, Sheep = Bend over head and eat grass and Mosquito = Find human!

*** Don't forget, that even if you reference a template, the class is still allowed to have its own properties and methods too! ***

We'll see how to use Polymorphism in the next section.

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan