Polymorphism using Overriding
Although overriding a function is similar to overloading a function, it is not exactly the same. Overriding a function is tied into class inheritance. In object oriented programming, should you not like the behavior of a given method in a base class, you simply override it in your derived class, making the method do what you want.
Look at Figure 1 again; notice that the Person class has a Play() method and derived class, and that Musician has a Play() method too. The second code listing shows that the Play() method of the class, Person, returns the string, "I am a Person playing," while the method Play() in Musician returns the string, "I am playing the " + Instrument, where Instrument is the private data member that indicates the instrument that the musician is playing. The Play() behavior in Musician is different than the Play() behavior in its base class, Person. The Play() method in Musician overrides the Play() method in Person.
Overriding in Java is a simple task—you simply use the same name of the function in the base class that you want to override. Java is smart enough to know how to do the override for you. The current version of VB does not support true inheritance. Thus, overriding a procedure in the base class isn’t even on the radar. However, Visual Basic 7 will support overrides with the Overrides keyword. The following listing shows how Visual Basic 7 intends to use the Overrides keyword. In this case, we are going to use the Overrides keyword to give new behavior to the Play() method in the Musician class. As in the Java example, the Musician class inherits from the Person class.
Option Explicit Inherits Person Private mvarMasteryLevel As Integer Private mvarInstrument As String . Overrides Function Play() Play() = "I am playing the " & mvarInstrument End Function . Public Property Let Instrument(ByVal vData As String) mvarInstrument = vData End Property Public Property Get Instrument() As String Instrument = mvarInstrument End Property Public Property Let MasteryLevel(ByVal vData As Integer) mvarMasteryLevel = vData End Property Public Property Get MasteryLevel() As Integer MasteryLevel = mvarMasteryLevel End Property . . .