overloads - why this code compile??? Please MS answer...

L

Laurent Allardin

Hi,

Why this code compile???

We should not be able to Override the code by using the Overloads since the
sub as exactly the same parameters....

Thank you.

Laurent Allardin,MCSD,MCAD
------------------------------

Public Class Base
Public Sub save()
Console.Write("save base")
End Sub
End Class

Public Class Derived
Inherits Base
Public Overloads Sub save()
Console.Write("save derived")
End Sub
End Class

Module Module1
Public Sub main()
Dim o As New derived
o.save()
End Sub

End Module
 
S

steve

| We should not be able to Override the code by using the Overloads since
the
| sub as exactly the same parameters....

first, overriding and overloading have nothing to do with eachother. the
former allows you to give new functionality to the base class' interface.
the latter allows polymorphism. you are not overriding the base class
interface. you should have not been able to compile though and should have
been warned to either override or shadow the derived class' matching method.
i assume the derived class' matching method definition of overload is only
amongst its members with the same name (differing parameters)...and not
overloading the base class.

does it complain when you try to compile without the overloads definition on
the derived class' method? what version of vb.net are you using? what
service packs are applied?
 
J

Jay B. Harlow [MVP - Outlook]

Laurent,
Why this code compile???
Why wouldn't it compile?

I agree I am as surprised as you are that it compiles, but I cannot really
come up with a good reason why it wouldn't.

In your specific sample I see Overloads as largely synonymous with Shadows,
so why disallow Overloads?

Yes Overloads & Shadows are two specific things with specific uses, however
in this instance I see them as the same thing... In other words concentric
circles and your sample is the very small overlap between the two circles...

If I have time later I will see what I can scrounge up to explain my
thinking here, no promises though.

Hope this helps
Jay
 
L

Larry Serflaten

Laurent Allardin said:
Why this code compile???

We should not be able to Override the code by using the Overloads since the
sub as exactly the same parameters....

That is called shadowing by name and signature. From the help file:

-----------
Derived classes can overload inherited members with members that have identical parameters and parameter types, a process known
as shadowing by name and signature. If the Overloads keyword is used when shadowing by name and signature, the derived classes
implementation of the member will be used instead of the implementation in the base class, and all other overloads for that
member will be available to instances of the derived class.

If the Overloads keyword is omitted when overloading an inherited member with a member that has identical parameters and
parameter types, then the overloading is called shadowing by name. Shadowing by name replaces the inherited implementation of a
member, and makes all other overloads unavailable to instances of the derived class, and its decedents.

--------------



HTH

LFS
 
S

steve

hey larry! does your participation mean that the inevitable shove from
vb.classic to vb.net has occured? either way, good to see you here!
 
L

Larry Serflaten

steve said:
hey larry! does your participation mean that the inevitable shove from
vb.classic to vb.net has occured? either way, good to see you here!

Yeah, the classics were getting a little perennial, or at least it wasn't really
providing as many new and exciting challenges as there are on this
side of the fence....

<g>
LFS
 

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