I come from a Java background. Please have a look at the code below (example taken from a Java book I once read and the code converted into .NET):
Public Class Animal
Public Overridable Sub Eat()
MsgBox("Animal Eat no arguement")
End Sub
End Class
Public Class Horse
Inherits Animal
Public Overrides Sub Eat()
MsgBox("Horse Eat no arguement")
End Sub
Public Overloads Sub Eat(ByVal food As String)
MsgBox("Horse Eat food arguement")
End Sub
End Class
Public Class Form1
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim a1 As New Animal
Dim a2 As New Horse
a1.Eat()
a2.Eat()
a2.Eat("Fruit") 'line 5
End Sub
End Class
I would expect line 5 of the form_load to produce a compile time error. In Java the compiler would look at the reference and see that Animal does not have an Eat method that takes a String. Why is there no compile error in .NET?
Update
There is an error in the code above. As the answerer points out; a2 is a reference to and an instance of Horse. Hence why line 5 does not cause a compile time error. If a2 referenced an animal and created an instance of a horse then there would be a compile time error (consistent with Java)
You are
overloadingeat()with a version that accepts astringarg in thehorseclass. That is entirely valid.An
overloadis a method with the same name but different arguments. Your overloadedeat(string)is perfectly valid and works just fine when called on an object and reference of typehorse.You could not call it on an object or reference of type
animal, though.