overides/loads usage

R

RdS

hi,

#1 i am looking through some code and have a question. The class I am
looking at is a derived class that has one constructor (new). it is coded
as:
public sub new ()
..
..
..
end sub

i see that the base class has a new procedure with no arguments. The
arguments in both base and derived classes are the same. Is the new
procedure in the derived class shadowing or overriding the new procedure in
base class? I understand that it would be easy to tell if the coder
specified keyword of overrides or shadows, but they didn't.

#2 In another derived class I see two new procedures. One with no
parameters like one in base class and the other with two parameters. I
understand that the constructor is overloaded, but is the new with no
parameters shadowing or overriding the new in base class.

How can one know if shadowing is being used or if overriding is being used
when no keyword is specified in procedure declaration?

Thanks in advance.
 
S

Scott M.

i see that the base class has a new procedure with no arguments. The
arguments in both base and derived classes are the same. Is the new
procedure in the derived class shadowing or overriding the new procedure
in base class? I understand that it would be easy to tell if the coder
specified keyword of overrides or shadows, but they didn't.

The answer is neither. Constructors are "special", in that, they neither
shadow or override other constructors from their base classes. Take this
example:

Public Class A
Sub New()

End Sub
End Class

Public Class B
Inherits A
Sub New()

End Sub
End Class

If you were to make an instance of "B", the constructor from class "A" will
fire and then the constructor from class "B".
#2 In another derived class I see two new procedures. One with no
parameters like one in base class and the other with two parameters. I
understand that the constructor is overloaded, but is the new with no
parameters shadowing or overriding the new in base class.

Nether again. The constructor in the derived class that takes the
parameters is overloading the constructor in the derived class AND the
constructor in the base class.
How can one know if shadowing is being used or if overriding is being used
when no keyword is specified in procedure declaration?

Simple. To use Shadowing or Overloading in VB.NET, the Shadows and
Overrideable and Overrides keywords MUST be specified. If you don't see
those words, they are not being used.
 
R

RdS

Thank you very much for responding.

#1 ok. I understand now. Is the new constructor the only procedure that
executes the new() in base before the derived?

#2 But why not always use overloading or shadowing rather than overriding?
It seems that you can accomplish what you need with overloading and
shadowing especially since to override (as you said) requires the
overridable keyword in base class. When do I use overloading verses
shadowing?

Thanks again.
 
S

Scott M.

#1 ok. I understand now. Is the new constructor the only procedure that
executes the new() in base before the derived?

Automatically, yes. You could make a base class's method run prior to the
derived class's method by inserting a MyBase.someMethod call in the derived
class's code.

Think of it this way, when you call any class's constructor, it is as if the
first line of code in that constructor is: MyBase.New.
#2 But why not always use overloading or shadowing rather than overriding?
It seems that you can accomplish what you need with overloading and
shadowing especially since to override (as you said) requires the
overridable keyword in base class. When do I use overloading verses
shadowing?

Overloading and Shadowing are 2 very different things. Overloading is for
when you want to have the same method name as an existing method, but want
it to be accesbale with different arguments than the original ALONG with the
original. The user can call either overloaded version they wish. The
overloaded version would still provide the same essential functionality, but
with different parameters. A common use is for default values vs. user
defined values. Take for example:

Public Sub New
x = 10
End Sub

Public Sub New(Amount As Integer)
x = Amount
End Sub

Both of these will cause x's value to change but one does it with a user
supplied value vs. a default value. This is overloading. Overriding would
mean that one TAKES THE PLACE of the other and only one could be called by
the user. Overloading allows EITHER to be called.

It's Overriding and Shadowing that are similar, but with one BIG difference.
Marking a class member as Overridable means that in derived classes, the
member can be re-writted (with the Overrides keyword) to take the place of
the Overridable member in the base class. The key here is that the derived
class can only re-write the member if the original member was declared
"Overridable". You need permission to use Overriding.

Sometimes you need to override something that was not originally marked as
Overridable and the original source code is not available. Then you
re-write the member in the derived class with the keyword "Shadows" to force
your derived member to override the base class member. This, however, must
only be done when necessary and with great care when it is done because you
could break some basic functionality by replacing a member with a completely
new implementation. Shadowing is overriding, just without the prior
permission to do so.

-Scott
 
R

RdS

Thank you very much Scott for your excellent explanations. It has helped
greatly. Have a good day.
 
B

Branco Medeiros

Scott M. wrote:
Sometimes you need to override something that was not originally marked as
Overridable and the original source code is not available. Then you
re-write the member in the derived class with the keyword "Shadows" to force
your derived member to override the base class member. This, however, must
only be done when necessary and with great care when it is done because you
could break some basic functionality by replacing a member with a completely
new implementation. Shadowing is overriding, just without the prior
permission to do so.
<snip>

Notice that in terms of the execution path there is a tremendous
difference bvetween Shadows and Overrides. Methods from the *base
class* will call an overriden method in a derived class, but not
shadowed ones:

Class Base
Overridable Sub Step1
Debug.Print("Base.Step1")
End Sub

Overridable Sub Step2
Debug.Print("Base.Step2")
End Sub

Sub DoIt
Debug.Print("Base.DoIt")
Step1
Step2
End Sub
End Class

Class Derived
Inherits Base
Overrides Sub Step1
Debug.Print("Derived.Step1")
End Sub

Shadows Sub Step2
Debug.Print("Derived.Step2")
End Sub

Shadows Sub DoIt
Debug.Print("Derived.DoIt")
Step1
Step2
End Sub
End Class

'...
Dim A As Base = New Derived
A.DoIt

The code fragment above will print:

Base.DoIt
Derived.Step1
Base.Step2

As you can see, even though the class is an instance of Derived, the
original Base.DoIt method is being called, because we declared A as a
Base. Because Derived shadows the original method, it would be
necessary to declare A as Derived to access Derived.DoIt.

Then the Base.DoIt method is able to call Derived.Step1. This is the
magic of overriden (aka 'virtual') methods: they allow for a base
class' method to be *altered* by a derived class.

Finally, we see that even though Base.Step2 is virtual (overridable),
the original method of Base is called, because Derived didn't
*override* it, just shadowed it.

In other words, overridable methods (with their respective overriding
in derived classes) allow for the modification of the original behavior
of the base class. It's as if you left from point A expecting to get at
point B and magically arrived at point C because of a shortcut that was
put in the road without your knowledge.

Shadowing, on the other side, just reuses a name from a base class,
effectivelly hiding that original name at the new scope. It's as if, to
get to point C, you had to go to a point named A, only that nobody
seems to remember that there was once a point A that lead to point B
instead, as if the original point A had never existed...


Regards,

Branco.
 

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