Information Technology Reference
In-Depth Information
public class DefaultMessageGenerator
{
public void Message()
{
Console .WriteLine( "This is a default message" );
}
}
public class AnotherMessageGenerator :
DefaultMessageGenerator , IMsg
{
// No explicit Message() method needed.
}
Notice that the derived class can declare the interface as part of its con-
tract, even though it does not provide any implementation of the IMsg
methods. As long as it has a publicly accessible method with the proper
signature, the interface contract is satisfied. Using this method, you can-
not use explicit interface implementation.
Implementing interfaces allows more options than creating and overrid-
ing virtual functions. You can create sealed implementations, virtual
implementations, or abstract contracts for class hierarchies. You can also
create a sealed implementation and provide virtual method calls in the
methods that implement interfaces. You can decide exactly how and when
derived classes can modify the default behavior for members of any inter-
face your class implements. Interface methods are not virtual methods but
a separate contract.
Item 24: Express Callbacks with Delegates
Me: “Son, go mow the yard. I'm going to read for a while.”
Scott: “Dad, I cleaned up the yard.”
Scott: “Dad, I put gas in the mower.”
Scott: “Dad, the mower won't start.”
Me: “I'll start it.”
Scott: “Dad, I'm done.”
This little exchange illustrates callbacks. I gave my son a task, and he
(repeatedly) interrupted me with the status. I did not block my own
progress while I waited for him to finish each part of the task. He was able
 
 
Search WWH ::




Custom Search