Yesterday I posted a question about the new/virtual/override keywords, and i learned a lot from your answers. But still i remain with some doubts.
In between all the
interface I1 { void Draw(); }
interface I2 { void Draw(); }
class A : I1, I2
{
// this is just a method in A
public void Minstance() { Console.WriteLine("A::MInstance"); }
// method in A, also implements I1.Draw. May be overridden in
// derived types.
public virtual void Draw() { Console.WriteLine("A::Draw"); }
// implements I2.Draw, accessible on object a of type A via ((I2)a).Draw()
void I2.Draw() { Console.WriteLine("A::I2.Draw"); }
}
class B : A, I1, I2
{
// new method in B, does not override A.Draw, so A.Draw is only
// callable on an object b of type B via ((A)b).Draw(). Types
// derived from B may override this method, but can't override
// A.Draw because it's hidden. Also implements I2.Draw (see notes).
public new virtual void Draw() { Console.WriteLine("B::Draw"); }
// implements I1.Draw, accessible on object b of type B via ((I1)b).Draw()
void I1.Draw() { Console.WriteLine("B::I2.Draw"); }
}
A class that inherits an interface implementation is permitted to re-implement the interface by including it in the base class list.
A re-implementation of an interface follows exactly the same interface mapping rules as an initial implementation of an interface. Thus, the inherited interface mapping has no effect whatsoever on the interface mapping established for the re-implementation of the interface. [Example: In the declarations
interface IControl
{
void Paint();
}
class Control: IControl
{
void IControl.Paint() {…}
}
class MyControl: Control, IControl
{
public void Paint() {}
}
the fact that
ControlmapsIControl.PaintontoControl.IControl.Paintdoesn’t affect the reimplementation inMyControl, which mapsIControl.PaintontoMyControl.Paint. end example]Inherited public member declarations and inherited explicit interface member declarations participate in the interface mapping process for re-implemented interfaces. [Example:
interface IMethods
{
void F();
void G();
void H();
void I();
}
class Base: IMethods
{
void IMethods.F() {}
void IMethods.G() {}
public void H() {}
public void I() {}
}
class Derived: Base, IMethods
{
public void F() {}
void IMethods.H() {}
}
Here, the implementation of
IMethodsinDerivedmaps the interface methods ontoDerived.F,Base.IMethods.G,Derived.IMethods.H, andBase.I. end example]When a class implements an interface, it implicitly also implements all that interface’s base interfaces. Likewise, a re-implementation of an interface is also implicitly a re-implementation of all of the interface’s base interfaces. [Example:
interface IBase
{
void F();
}
interface IDerived: IBase
{
void G();
}
class C: IDerived
{
void IBase.F() {…}
void IDerived.G() {…}
}
class D: C, IDerived
{
public void F() {…}
public void G() {…}
}
Here, the re-implementation of
IDerivedalso re-implementsIBase, mappingIBase.FontoD.F. end example]