Information Technology Reference
In-Depth Information
Derived classes can also prevent further overrides by sealing the method:
public class
MyDerivedClass2
:
MyClass
{
public sealed override void
Message()
{
Console
.WriteLine(
"MyDerivedClass"
);
}
}
Another solution is to implement the interface, and include a call to a vir-
tual method that enables derived classes to participate in the interface con-
tract. You would do that in MyClass this way:
public class
MyClass2
:
IMsg
{
protected virtual void
OnMessage()
{
}
public void
Message()
{
OnMessage();
Console
.WriteLine(
"MyClass"
);
}
}
Any derived class can override OnMessage() and add their own work to the
Message() method declared in MyClass2. It's a pattern you've seen before
when classes implement IDisposable (see Item 17).
Explicit interface implementation (see Item 31) enables you to implement
an interface, yet hide its members from the public interface of your type.
Its use throws a few other twists into the relationships between imple-
menting interfaces and overriding virtual functions. You use explicit inter-
face implementation to limit client code from using the interface methods
when a more appropriate version is available. The IComparable idiom in
Item 31 shows this in detail.
There is also one last wrinkle to add to working with interfaces and base
classes. A base class can provide a default implementation for methods in an
interface. Then, a derived class can declare that it implements an interface
and inherit the implementation from that base class, as this example shows.