
44
|
Chapter 3: Introduction to Object-Oriented Programming
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
useAsBase = secondTry
useAsBase.ShowLocation( ) ' --> Shows "Derived", "Base"
The first call to ShowLocation is made using a variable of type BaseClass that refers
to an object of type
BaseClass. In this case, both of the calls:
Me.WhereAmI( )
MyClass.WhereAmI( )
return the same value, because they both call WhereAmI in BaseClass.
However, in the second case, the variable of type
BaseClass holds a reference to an
object of
DerivedClass. In this case, Me refers to an object of type DerivedClass (the
secondTry reference), whereas MyClass still refers to the base class BaseClass (the
useAsBase reference). When using the Me keyword, the actual object as originally
instantiated is used; when using
MyClass, the class of the variable that is used to
make the method call becomes the controlling class.
Shadowing and Overloading Members
Visual Basic provides a few additional features that let you provide even more
control over which members are used in your base and derived classes.
Shadowing
Shadowing is similar to overriding, but with some very important differences.
Consider two classes,
BaseClass and DerivedClass:
Public Class BaseClass
Public simpleField As Integer = 1
Public Overridable Sub TestOverride( )
MsgBox("BaseClass:TestOverride") ...