Overriding in VB.NET
What is overriding in VB.NET?
Explanation
Over riding in VB.NET
Overriding in VB.net is method by which a inherited property or a method is overidden to perform a different functionality in a derived class.
The base class function is declared using a keyword Overridable and the derived class function where the functionality
is changed contains an keyword Overrides.
Example:
Module Module1
Class Over
Public Overridable Function add(ByVal x As Integer,
ByVal y As Integer)
Console.WriteLine('Function Inside Base Class')
Return (x + y)
End Function
End Class
Class DerOver
Inherits Over
Public Overrides Function add(ByVal x As Integer,
ByVal y As Integer)
Console.WriteLine(MyBase.add(120, 100))
Console.WriteLine('Function Inside Derived Class')
Return (x + y)
End Function
End Class
Sub Main()
Dim obj As New DerOver
Console.WriteLine(obj.add(10, 100))
Console.Read()
End Sub
End Module
Result:
Function Inside Base Class
220
Function Inside Derived Class
110
Description:
In the above overriding example the base class function
add is overridden in the derived class
using the
MyBase.add(120,100) statement. So first the overidden value is displayed, then the value from the
derived class is displayed.