Allows a property or method in a class to be overridden in a derived class.
Public Class Person
Public Overridable Sub DoSomething()
Console.WriteLine("Person")
End Sub
End Class
Overrides an Overridable property or method defined in the base class.
Public Class Customer
Inherits Person
'Base Class must be Overridable
Public Overrides Sub DoSomething()
Console.WriteLine("Customer")
End Sub
End Class
Prevents a property or method from being overridden in an inheriting class. Default behaviour. Can only be declared on overrides methods
Public Class Person
Public Overridable Sub DoSomething()
Console.WriteLine("Person")
End Sub
End Class
Public Class Customer
Inherits Person
Public NotOverridable Overrides Sub DoSomething()
Console.WriteLine("Customer")
End Sub
End Class
Public Class DetailedCustomer
Inherits Customer
'DoSomething can't be overridden
End Class
Example Usage:
Dim p As New Person
p.DoSomething()
Dim c As New Customer
c.DoSomething()
Dim d As New DetailedCustomer
d.DoSomething()
Output:
Person
Customer
Customer
Requires that a derived class override the property or method.
MustOverride methods must be declared in MustInherit classes.
Public MustInherit Class Person
Public MustOverride Sub DoSomething()
'No method definition here
End Class
Public Class Customer
Inherits Person
'DoSomething must be overridden
Public Overrides Sub DoSomething()
Console.WriteLine("Customer")
End Sub
End Class
Example Usage:
Dim c As New Customer
c.DoSomething()
Output:
Customer