469,366 Members | 2,243 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

Overloads Overrides

Hi
I am here to solve a small confusion i have in "Overloads Overrides".

"Overloading" says that the method's name should be same while no. of
parameters and/or their datatypes should be changed either in the same
class or inherited class. right?

"Overriding" says that the method's signature should be same(name,no.
of parameters and their datatypes) in the inherited class while
implementation may change. right?

How we can implement both of these all at the same time like

[Visual Basic.NET]
Overloads Overrides Public Sub DoSomething()

Here, if we change the signature we can't implement Overriding and if
we don't change signature then we can't implement Overloading BUT It
is happening. How?
Has VB.NET changed the concept of overloading and overriding or
I AM WRONG :(: ?

Ref:
http://msdn.microsoft.com/library/de...elinetopic.asp

Would someone make it clear how this is being done.
Thanks
Nov 21 '05 #1
10 13517
Atif,
Overloads Overrides Public Sub DoSomething() Says that you are overriding MyBase.DoSomething() with no parameters, while
a MyBase.DoSomething(...) or MyClass.DoSomething(...) with parameters
exists.

Hope this helps
Jay

"Atif" <at*******@hotmail.com> wrote in message
news:3c**************************@posting.google.c om... Hi
I am here to solve a small confusion i have in "Overloads Overrides".

"Overloading" says that the method's name should be same while no. of
parameters and/or their datatypes should be changed either in the same
class or inherited class. right?

"Overriding" says that the method's signature should be same(name,no.
of parameters and their datatypes) in the inherited class while
implementation may change. right?

How we can implement both of these all at the same time like

[Visual Basic.NET]
Overloads Overrides Public Sub DoSomething()

Here, if we change the signature we can't implement Overriding and if
we don't change signature then we can't implement Overloading BUT It
is happening. How?
Has VB.NET changed the concept of overloading and overriding or
I AM WRONG :(: ?

Ref:
http://msdn.microsoft.com/library/de...elinetopic.asp

Would someone make it clear how this is being done.
Thanks

Nov 21 '05 #2
Jay B. Harlow [MVP - Outlook] wrote:
Atif,
Overloads Overrides Public Sub DoSomething()


Says that you are overriding MyBase.DoSomething() with no parameters, while
a MyBase.DoSomething(...) or MyClass.DoSomething(...) with parameters
exists.


The compiler does not agree with you:

Public Class Foo
Public Overrides Function Equals(ByVal other As Object) As Boolean
End Function
End Class

The compiler issues the following warning for this code:

warning BC40003: function 'Equals' shadows an overloadable member declared in
the base class 'Object'. If you want to overload the base method, this method
must be declared 'Overloads'.

The only Equals() methods that are involved are the ones on System.Object and Foo,
and no Equals() with different parameters exists.

Yet, I get the warning unless I write:

Public Overloads Function Equals(ByVal other As Object) As Boolean

However, I'm not overloading Equals, I'm overriding it: the function signature
in the derived class matches the function signature in the base class.

Another way to get rid of the warning is to use:

Public Overloads Overrides Function Equals(ByVal other As Object) As Boolean

But, again, I'm not overloading anything, I'm overriding the base class method.

It seems that the compiler is in conflict with the documentation, which says:

Overloads
Optional. Indicates that this Function procedure overloads one or more
procedures defined with the same name in a base class. The argument list
in this declaration must be different from the argument list of every
overloaded procedure. The lists must differ in the number of arguments,
their data types, or both. This allows the compiler to distinguish which
version to use.

Overrides
Optional. Indicates that this Function procedure overrides an identically
named procedure in a base class. The number and data types of the arguments,
and the data type of the return value, must exactly match those of the base
class procedure.

So, overloading does not apply in case of Equals, yet the compiler issues the warning
unless the Overloads keyword is present. Seems that the compiler needs fixing?

Cheers,

Michi.
Nov 21 '05 #3
Jay
Overloads Overrides Public Sub DoSomething()
Says that you are overriding MyBase.DoSomething() with no parameters, while
a MyBase.DoSomething(...) or MyClass.DoSomething(...) with parameters
exists.


Let us suppose these three methods are there in MyBase
1- DoSomething()
2- DoSomething(a as integer)
3- DoSomething(a as integer, b as integer)

If you write
Overrides Public Sub DoSomething(a as integer)
you are specifying exactly which method you are overriding i.e. 2nd,
no need to add Overloads as there is no overloading
Michi
The compiler does not agree with you:
. . . Seems that the compiler needs fixing?


I agree, but, we require more comments
Nov 21 '05 #4
Michi,
System.Object has overloaded Equals.

http://msdn.microsoft.com/library/de...qualsTopic.asp

Overloads Public Overridable Function Equals(Object) As Boolean
Overloads Public Shared Function Equals(Object, Object) As Boolean
Hence the error, and hence the need for the Overloads keyword.

Hope this helps
Jay

"Michi Henning" <mi***@zeroc.com> wrote in message
news:41**************@zeroc.com...
Jay B. Harlow [MVP - Outlook] wrote:
Atif,
Overloads Overrides Public Sub DoSomething()


Says that you are overriding MyBase.DoSomething() with no parameters,
while a MyBase.DoSomething(...) or MyClass.DoSomething(...) with
parameters exists.


The compiler does not agree with you:

Public Class Foo
Public Overrides Function Equals(ByVal other As Object) As Boolean
End Function
End Class

The compiler issues the following warning for this code:

warning BC40003: function 'Equals' shadows an overloadable member
declared in
the base class 'Object'. If you want to overload the base method, this
method
must be declared 'Overloads'.

The only Equals() methods that are involved are the ones on System.Object
and Foo,
and no Equals() with different parameters exists.

Yet, I get the warning unless I write:

Public Overloads Function Equals(ByVal other As Object) As Boolean

However, I'm not overloading Equals, I'm overriding it: the function
signature
in the derived class matches the function signature in the base class.

Another way to get rid of the warning is to use:

Public Overloads Overrides Function Equals(ByVal other As Object) As
Boolean

But, again, I'm not overloading anything, I'm overriding the base class
method.

It seems that the compiler is in conflict with the documentation, which
says:

Overloads
Optional. Indicates that this Function procedure overloads one or more
procedures defined with the same name in a base class. The argument list
in this declaration must be different from the argument list of every
overloaded procedure. The lists must differ in the number of arguments,
their data types, or both. This allows the compiler to distinguish which
version to use.

Overrides
Optional. Indicates that this Function procedure overrides an identically
named procedure in a base class. The number and data types of the
arguments,
and the data type of the return value, must exactly match those of the
base
class procedure.

So, overloading does not apply in case of Equals, yet the compiler issues
the warning
unless the Overloads keyword is present. Seems that the compiler needs
fixing?

Cheers,

Michi.

Nov 21 '05 #5
Atif,
If you write
Overrides Public Sub DoSomething(a as integer)
you are specifying exactly which method you are overriding i.e. 2nd,
no need to add Overloads as there is no overloading Correct, you are specifying exactly which method you are overriding.

HOWEVER!!! you are also overloading base #1 & base #3! With out the
Overloads on your derived #2, the compiler will assume you wanted to Shadow
base #1 & base #3 when you shadow #1 & #3 they are not available from
variables of your derived class...

Hope this helps
Jay
"Atif" <at*******@hotmail.com> wrote in message
news:3c*************************@posting.google.co m... Jay
>>Overloads Overrides Public Sub DoSomething()
>
> Says that you are overriding MyBase.DoSomething() with no parameters,
> while
> a MyBase.DoSomething(...) or MyClass.DoSomething(...) with parameters
> exists.


Let us suppose these three methods are there in MyBase
1- DoSomething()
2- DoSomething(a as integer)
3- DoSomething(a as integer, b as integer)

If you write
Overrides Public Sub DoSomething(a as integer)
you are specifying exactly which method you are overriding i.e. 2nd,
no need to add Overloads as there is no overloading
Michi
The compiler does not agree with you:
. . . Seems that the compiler needs fixing?


I agree, but, we require more comments

Nov 21 '05 #6
Jay B. Harlow [MVP - Outlook] wrote:
Michi,
System.Object has overloaded Equals.

http://msdn.microsoft.com/library/de...qualsTopic.asp

Overloads Public Overridable Function Equals(Object) As Boolean
Overloads Public Shared Function Equals(Object, Object) As Boolean
Hence the error, and hence the need for the Overloads keyword.


Ah, that makes sense. Thanks for that!

Cheers,

Michi.
Nov 21 '05 #7
Jay B. Harlow [MVP - Outlook] wrote:
Michi,
System.Object has overloaded Equals.

http://msdn.microsoft.com/library/de...qualsTopic.asp

Overloads Public Overridable Function Equals(Object) As Boolean
Overloads Public Shared Function Equals(Object, Object) As Boolean
Hence the error, and hence the need for the Overloads keyword.


Ah, that makes sense. Thanks for that!

Cheers,

Michi.
Nov 21 '05 #8
> HOWEVER!!! you are also overloading base #1 & base #3! With out the
Overloads on your derived #2, the compiler will assume you wanted to Shadow
base #1 & base #3 when you shadow #1 & #3 they are not available from
variables of your derived class...


GOT IT :)
Thanks, Thanks alot
Nov 21 '05 #9
> HOWEVER!!! you are also overloading base #1 & base #3! With out the
Overloads on your derived #2, the compiler will assume you wanted to Shadow
base #1 & base #3 when you shadow #1 & #3 they are not available from
variables of your derived class...


GOT IT :)
Thanks, Thanks alot
Nov 21 '05 #10
Ok,
If you have overloaded a method of the base class in your class and
then you are Overriding any overloaded version of that method, either
in your class or in the base class, you have to use both Overloads
Overrides.

Why do this?

As far as Shadowing is concern if i don't use Overloads(in Overloads
Overrides), I am clearly specifying that i am Overriding the methos.
If i ommit both the words(Overloads Overrides) then it will shadow the
method in base class otherwise it shouldn't.

How VB.NET's Compiler implements these concepts(as far as calling a
nethod is concern) i.e. u should use Overloads otherwise it will
shadow....if not overloading the method then can use Overrides
otherwise should use Overloads Overrides.... :)):

Any help = appreciated^2
Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Lee Silver | last post: by
3 posts views Thread by Arthur Dent | last post: by
2 posts views Thread by =?Utf-8?B?QU1lcmNlcg==?= | last post: by
reply views Thread by Internet User | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.