Interface implementation Inheritance Mechanism
A class inherits the implementation of interfaces provided by its base class:
Note: If you do not explicitly implement the interface again, the derived class cannot change the interface ing inherited from the base class.
Using System;
Interface IControl
{
Void Paint ();
}
Class Control: IControl
{
Public void Paint ()
{
Console. WriteLine ("Control invoke! ");
}
}
Class TextBox: Control
{
New public void Paint ()
{
Console. WriteLine ("TextBox invoke! ");
}
}
Class Invoke
{
Public static void Main ()
{
Control c = new Control ();
TextBox tb = new TextBox ();
IControl ic = c;
IControl it = tb;
C. Paint (); // invokes Control. Paint ();
Tb. Paint (); // invokes TextBox. Paint ();
Ic. Paint (); // invokes Control. Paint ();
It. Paint (); // invokes Control. Paint ();
}
}
However, when an interface method is mapped to a virtual method in the class, the derived class can overload this virtual method and change the implementation of this interface:
Interface IControl
{
Void Paint ();
}
Class Control: IControl
{
Public virtual void Paint ()
{
}
}
Class TextBox: Control
{
Public override void Paint ()
{
}
}
Code effect:
Control c = new Control ();
TextBox tb = new TextBox ();
IControl ic = c;
IControl it = tb;
C. Paint (); // call Control. Paint ();
Tb. Paint (); // call TextBox. Paint ();
Ic. Paint (); // call Control. Paint ();
It. Paint (); // call TextBox. Paint ();
Because the explicitly stated interface members cannot be declared as virtual, the implementation of explicitly stated interfaces cannot be reloaded:
Note: We recommend that you use an explicitly stated interface to call another method. The called method can be declared as virtual and allowed to be reloaded by the derived class.
Interface IControl
{
Void Paint ();
}
Class Control: IControl
{
Void IControl. Paint ()
{
PaintControl ();
}
Protected virtual void PaintControl ()
{
}
}
Class TextBox: Control
{
Protected override void PaintControl ()
{
}
}
Here, classes derived from Control can be implemented by reloading the PaintControl method. Paint.
Referenced from:
Http://www2.cnblogs.com/netfork/archive/2004/03/23/3930.html