Mixing explicit implementation and polymorphism?

  • Thread starter Thread starter puzzlecracker
  • Start date Start date
P

puzzlecracker

Hello,

I am getting these errors:
===================
1) " type Bar does not implement interface IFoo"

2)and cannot call base.DoA();


From this code:
===========



public interface IFoo
{
void DoA();

void DoB();
}

public class Foo:IFoo
{

void IFoo.DoA()
{

}

void IFoo.DoB()
{


}
}

public class Bar:Foo
{

void IFoo.DoA()
{
base.doA();

}

void IFoo.DoB()
{
base.doB();

}
}

I am using C# 2.0.

What am I doing wrong? How to fix this? ThaNKS
 
That's because it doesn't.  A class only implements an interface if it's  
declared as doing so.  You've only declared Foo as implementing IFoo, not  
Bar.

Just to clarify this (since it can easily be taken wrongly). An
instance of Bar from the code sample above still implements IFoo; that
is, you can still write:

IFoo f = new Bar();

However, to explicitly implement members of an interface, the class
must implement that interface directly, and not via a base class.
 
Peter said:
That's because it doesn't. A class only implements an interface if
it's declared as doing so. You've only declared Foo as implementing
IFoo, not Bar.


That's because there is no "base.DoA()". There is only
"((IFoo)base).DoA()". You've implemented the interface explicitly,

That's not at all the same. I don't think there's any way to call a base
explicit implementation when a derived class has implemented the same
interface -- the only access to the base implementation was through the
interface v-table, and the interface v-table contains the most derived
implementation only. OTOH with implicit implementation, the base class
declares a public member function that can be called by the derived class.
 
Back
Top