473,329 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,329 software developers and data experts.

Overriding

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
Feb 3 '07 #1
5 1277
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?
Feb 3 '07 #2

"Roshawn" <ra*********@yahoo.comwrote in message
news:eI**************@TK2MSFTNGP04.phx.gbl...
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.
Feb 3 '07 #3
On Feb 2, 8:15 pm, Roshawn <radawson...@yahoo.comwrote:
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.

--
Tom Shelton

Feb 3 '07 #4
Thanks Tom. I guess I'll do some reading with regard to Shadowing.

Tom Shelton wrote:
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.

--
Tom Shelton
Feb 3 '07 #5
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.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Roshawn" <ra*********@yahoo.comwrote in message
news:O$**************@TK2MSFTNGP04.phx.gbl...
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
Feb 4 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
3
by: Amin Sobati | last post by:
Hi, I have two classes. Class2 inhertis Class1: ----------------------------- Public Class Class1 Public Overridable Sub MySub() End Sub End Class Public Class Class2
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.