Library tutorials & articles
Polymorphism in VB
- Introduction
- Polymorphism and Interfaces
- Using it (1)
- Using it (2)
- Why bother?
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.
Related articles
Related discussion
-
Read eMails from Outlook express using ASP
by kumaravelu (1 replies)
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
Related podcasts
-
Scott Guthrie
Scott catches up with Scott Guthrie in an interview covering Ajax, Asp 2.0, extender controls, CSS adapters and more.
If you need to access methods specific to that object, then you need to declare a variable for that object. For instance,
Dim obj As myActualObject
Set obj = New myActualObject
rather than the interface that myActualObject might be implementing...
good stuff this
Hi there,
Great article, very interesting. But when I tried to implement the idea it worked well until. When trying to create public methods in the Implementing Classes of the Interface other than the methods provided by the interface I stumbled upon a problem.
The autocompletion of VB6 could not find the methods, why is that? And when I type in the method by hand and run the app I get the error: Method or Data member not found
Which means that inspite of which type of Class I initialize I only have access to those methods that are implemented in the Interface class, is there a way around it? Or have I totally missunderstood it?
Rgds Sparvhok
This thread is for discussions of Polymorphism in VB.