
Object-Oriented Features
|
87
End Sub
End Class
In this case, the code in the constructor for Account calls the
Account version of MakeDeposit. Note that
MyClass is not
the same as
Me in VB. Using Me would still call the derived
version instead of the original version.
Requiring/Preventing Inheritance
In C# you can mark a class as sealed. A sealed class is a class
that cannot be used as a base class for another class—in
other words, you cannot inherit from it. Here is an example
of adding the word “sealed” to the class definition:
class Account
{
}
sealed class Checking : Account
{
}
//***this is illegal***
class SuperChecking : Checking
{
}
In this code example, Account is not sealed, so Checking can
inherit from it; however, Checking is a sealed class, so it is
illegal for SuperChecking to inherit from it. This is not the
place to have a full discussion of why you would seal, but
normally you would seal a class to prevent someone from
writing a subclass that overrides how you implemented an
interface. Take for example the System.String class that
Microsoft provides. It implements a series of critical inter-
faces: ICloneable, IComparable, etc. The runtime expects
these interfaces to be implemented in a particular way. It
would cause problems if you were to write your own string
class and override the way those interfaces are implemented,
and then pass instances of your string class to functions in
the runtime