Scott M. wrote:
<snip>
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.