Why is overloading called compile time polymophism and overriding called runtime polymorphism? For example, have a look at the code below:
Public Class Animal
Public Overridable Overloads Sub Eat()
MsgBox("Animal Eat no arguement")
End Sub
Public Overridable Sub Drink()
MsgBox("Animal drink arguement")
End Sub
End Class
Public Class Horse
Inherits Animal
Public Overloads 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
Public Overloads Overrides Sub Drink()
MsgBox("Animal drink 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 Animal
a2 = New Horse
a1.Eat()
a2.Eat("Fruit") 'line 6
End Sub
End Class
Line 6 will cause a compile time error as the program stands. However, if I add an Eat(String) to the animal class then it will compile. What is the reasoning behind this?
Also the answer in the following post says: "The Overloads keyword is optional, but if you use it for one method, you must use it for all overloads of that method: "http://stackoverflow.com/questions/1173257/overloads-keyword-in-vb-net. I am not always finding this to be the case, if the function in question also Overrides. Is this the case?
I am looking through a large program that uses polymophism with interfaces. I have supplied the class above as an example for illustration purposes.