Overriding

R

Roshawn

Hi,

Is it possible to override a property, function, or sub in a base class if the Overridable
keyword isn't specified?

For example:

Public Class ExampleA
Sub DoSomething()
Dim str as String = "Do something already"
End Sub
End Class

Public Class ExampleB
Inherits ExampleA

Overrides Sub DoSomething()
Dim str as String = "Already doing something"
End Sub
End Class


Would this work?

Thanks,
Roshawn
 
R

Roshawn

In addition, can I override a property that's already been overridden?

For example:

Public Class One
Overridable Sub DoSomething()
Dim str as String = "Do something already"
End Sub
End Class

Public Class Two
Inherits One
Overrides Sub DoSomething()
Dim str as String = "Already doing something"
End Sub
End Class

Public Class Three
Inherits Two
Overrides Sub DoSomething()
Dim str as String = "Won't stop until I get enough!"
End Sub
End Class


Would that work?
 
P

pvdg42

Roshawn said:
In addition, can I override a property that's already been overridden?

For example:

Public Class One
Overridable Sub DoSomething()
Dim str as String = "Do something already"
End Sub
End Class

Public Class Two
Inherits One
Overrides Sub DoSomething()
Dim str as String = "Already doing something"
End Sub
End Class

Public Class Three
Inherits Two
Overrides Sub DoSomething()
Dim str as String = "Won't stop until I get enough!"
End Sub
End Class


Would that work?

Yes, as the overrides are in different subclasses.
 
T

Tom Shelton

Hi,

Is it possible to override a property, function, or sub in a base class if the Overridable
keyword isn't specified?

For example:

Public Class ExampleA
Sub DoSomething()
Dim str as String = "Do something already"
End Sub
End Class

Public Class ExampleB
Inherits ExampleA

Overrides Sub DoSomething()
Dim str as String = "Already doing something"
End Sub
End Class

Would this work?

Thanks,
Roshawn

Here is a quick sample:

Option Strict On
Option Explicit On

Module Module1

Sub Main()
Dim one As A = New A
' call an A through an A reference
one.DoStuff()
' Output = "In A.DoStuff"

' call an B through an A reference
Dim two As A = New B()
two.DoStuff()
' Output = "In A.DoStuff (not what you would expect?)"

' call an C through an A reference
Dim three As A = New C
three.DoStuff()
' Output = "In A.DoStuff (not what you would expect?)"

' Call an B through an B reference
Dim four As B = New B
four.DoStuff()
' Output = "In B.DoStuff"

' call an C through an C reference
Dim five As B = New C
five.DoStuff()
' Output = "In C.DoStuff"
End Sub

Class A
Public Sub DoStuff()
Console.WriteLine("In A.DoStuff")
End Sub
End Class

Class B
Inherits A
Public Overridable Shadows Sub DoStuff()
Console.WriteLine("In B.DoStuff")
End Sub
End Class

Class C
Inherits B

Public Overrides Sub DoStuff()
Console.WriteLine("In C.DoStuff")
End Sub
End Class

End Module

If you do not declare a sub/function/property overrideable, then sure
you can override it in the subclass, but you will not get polymorphic
behavior either. This is shadowing. You can find documentation on
it.
 
J

Jay B. Harlow [MVP - Outlook]

Roshawn,
As Tom suggests you can shadows a base method that was not marked
overridable.

HOWEVER!! Avoid creating a design that requires the use of shadows, as are
"anti-polymorphic", while Overrides are polymorphic.
Public Class ExampleB
Inherits ExampleA

Shadows Sub DoSomething()
Dim str as String = "Already doing something"
End Sub
End Class

For example:

Dim a As ExampleA = New ExampleA
a.DoSomething()

' Now create an ExampleA variable containing a derived object
Dim b As ExampleA = New ExampleB
b.DoSomething()

Notice that str is still "Do something already" as DoSomething is not
polymorphic, b is defined as an ExampleA type, so you get ExampleA behavior.
Overridable indicates you want polymorphism.


To continue your second example, Two can override a method such that derived
classes can no longer override them.
Public Class Two
Inherits One
Overrides NotOverridable Sub DoSomething()
Dim str as String = "Already doing something"
End Sub
End Class

This is useful when Two needs to dictate what the expected behavior will be,
without allowing derived classes to modify it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top