Where you define or implement a virtual method in a parent class and then replace it in a descendant class.
When you decide to declare a method as virtual, you are giving permission to derived classes to extend and override the method with their own implementation. You can have the extended method call the parent method's code too.
In most OO languages you can also choose to hide a parent method. When you introduce a new implementation of the same named method with the same signature without overriding, you are hiding the parent method.
VB.Net Overriding
In VB.Net, you specify a virtual method with the Overridable keyword in a parent class and extend (or replace) it in a descendant class using the Overrides keyword.
Use the base keyword in the descendant method to execute the code in the parent method, i.e. base.SomeMethod().
Override Details
- You cannot override a regular non-virtual method, nor a static method.
- The first version of the parent method must be Overridable or MustOverride.
- You can override any parent method marked Overridable, MustOverride, or Overrides (already overridden).
- The methods must have the same signature.
- The methods must have the same visibility (the same access level).
- Use the base keyword to refer to the parent class as in MyBase.SomeMethod().
Working VB.Net 2008 Override Example
The following code assumes a Windows application with a single form with a button. It demonstrates using Overridable and Overrides to override a parent method in a descendant class.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyRobot As Robot
Dim MyCyborg As Cyborg
MyRobot = New Robot
MyCyborg = New Cyborg
MyRobot.Speak()
MyCyborg.Speak()
End Sub
End Class
Public Class Robot
Public Overridable Sub Speak()
MessageBox.Show("Robot says hi")
End Sub
End Class
Public Class Cyborg
Inherits Robot
Public Overrides Sub Speak()
MessageBox.Show("hi")
End Sub
End Class
Hiding a Method with Shadows
Use the Shadows keyword to introduce a new implementation of a parent method (this hides the parent method). You can hide a method without using Shadows but you will get a compiler warning. Using Shadows will suppress the warning.
The Shodaws and Overrides modifiers have different meanings. The Shadows modifier creates a new member with the same name, signature, and visibility and hides the original member. The Overrides modifier extends the implementation for an inherited member and allows you to implement inheritance-based polymorphism.
Avoid Introducing New Members: Sometimes there are clear reasons to use Shadows to introduce a new method with with the same name, signature, and visibility of a parent method. In those clear cases, introducing a new member is a powerful feature. However, if you do not have a clear reason, then avoid introducing a new version of a method by naming the new method something unique and appropriate.
Public Class Robot
Public Sub Speak()
MessageBox.Show("Robot says hi")
End Sub
End Class
Public Class Cyborg
Inherits Robot
Public Shadows Sub Speak()
MessageBox.Show("hi")
End Sub
End Class
Calling the Parent Class Version with MyBase
A common task In OO is to extend a method by first executing the parent method code and then adding code. Use the MyBase keyword to refer to the parent class as in MyBase.SomeMethod().
Public Class Robot
Public Overridable Sub Speak()
MessageBox.Show("Robot says hi")
End Sub
End Class
Public Class Cyborg
Inherits Robot
Public Overrides Sub Speak()
MyBase.Speak()
MessageBox.Show("hi")
End Sub
End Class