4

I just work through a book about OOP (Object-Oriented Programming with visual Basic .NET by J.P.Hamilton). At a point where he has explained overloading but not yet override he writes: "You can only overload methods in a class; this is, you can add new methods that have the same name as existing methods. However, existing behavior cannot be redefined."

But as far as I understand you can redefine a method from a child class using overload like:

Public Class clsTest
    Sub addition(ByVal x As Integer)
        MsgBox(x)
    End Sub
End Class

Public Class clsTest2 : Inherits clsTest
    Overloads Sub addition(ByVal x As Integer)
        MsgBox(x * x)
    End Sub
End Class

If this is correct, then I would like to know, when to use overload and when to use override to redifine a method, since

Public Class clsTest
    Overridable Sub addition(ByVal x As Integer)
        MsgBox(x)
    End Sub
End Class

Public Class clsTest2 : Inherits clsTest
    Overrides Sub addition(ByVal x As Integer)
        MsgBox(x * x)
    End Sub
End Class

gives me the same output.

walther
  • 13,466
  • 5
  • 41
  • 67
ruedi
  • 5,365
  • 15
  • 52
  • 88
  • Your first example does not show me what I understand to be "overloading" of anything. Is there something missing from it? – ClickRick May 25 '14 at 18:16
  • http://stackoverflow.com/questions/1173257/overloads-keyword-in-vb-net – CodeCaster May 25 '14 at 18:22
  • Maybe I just dont get it but I am a beginner so sorry in advance but in the first example i overload the base class method from the child class using the same method name and the same signiture. That is overloading isnt it? I know that Overloads is optional but that is not the point here isnt it? The point is, if I can achieve the same thing with two different concepts, what should I use? – ruedi May 25 '14 at 18:25
  • 2
    That is not overloading as I understand it. This looks like one of those VB.NET cases: http://stackoverflow.com/a/7880470/745969 – Tim May 25 '14 at 18:29
  • Thanks Tim! Ok, according to the answer you posted: "If you use the OVERLOADS keyword instead, it will only hide the base class method with an identical signature." So the result is the same but something completley different is happening. But since the result is the same, what is the consequence. Next time I want to change the behavior of a base class method (in case the child class has the same method with same signeture), should I use the first or the second concept? – ruedi May 25 '14 at 18:40

1 Answers1

4

That first snippet made me exclaim "Huh?" I had to look it up in the VB.NET Language Specification. It is indeed valid, it is shadowing, hiding inherited names. You'd normally use the Shadows keyword for that, but Overloads is correct too, it does a different kind of hiding. Shadows only hides an inherited member with the exact same signature, Overloads hides everything with the same name, including all the overloads with the same name in the base class.

It is not very clear to me if shadowing is what you really had in mind. It is pretty dangerous, it has a knack of getting the wrong method called. You get the method that the compiler thinks you want to call, based on the object reference type. The quirky case is:

  Dim obj As ClsTest
  obj = new ClsTest2
  obj.addition(42)     '' Calls ClsTest.addition

This is not often intended, you'd expect ClsTest2.addition() to be called since the object is actually of type ClsTest2. A problem that's avoided when you use a virtual method, like you did in the second snippet. You then get ClsTest2.addition(), regardless of the object reference type. If you are not sure what you want then pick this one.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • uh, i would expect the base class to be called ;) shadows is handy for overriding base properties/members of default winforms controls. you can always call the base member from the shadowed copy. but that is a very nice example :) – porkchop May 26 '14 at 02:08