Overloads Overrides

  • Thread starter Thread starter Atif
  • Start date Start date
A

Atif

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/d...ilerindentedtextwriterclasswritelinetopic.asp

Would someone make it clear how this is being done.
Thanks
 
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 said:
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/d...ilerindentedtextwriterclasswritelinetopic.asp

Would someone make it clear how this is being done.
Thanks
 
Jay said:
Atif,


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.
 
Jay

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
 
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
 
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
 
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
 
Back
Top