WhereDidTheInheritedInterfaceGo?

  • Thread starter Thread starter Carl Johansson
  • Start date Start date
C

Carl Johansson

namespace WhereDidTheInheritedInterfaceGo
{
class MainClass
{
static void Main(string[] args)
{
AnInterface ai = new DerivedClass();
ai.InterfaceMethod();
}
}

interface AnInterface
{
void InterfaceMethod();
}

class ImplementingClass : AnInterface
{
void AnInterface.InterfaceMethod()
{
}
}

class DerivedClass : ImplementingClass, AnInterface
{
void AnInterface.InterfaceMethod()
{
//Would it be possible to call the inherited
//InterfaceMethod here or has it been replaced by this
//implementation and therefore ceased to exist?
}
}
}

//Regards Carl Johansson
 
Adam Benson said:
base.InterfaceMethod() ??

- Adam.
=======

No, unfortunately not! The problem is that 'base' is a reference to
ImplementingClass. What we need is a reference to AnInterface of
ImplementingClass, I guess. I've been trying to cast 'base' in several ways,
but the compiler just won't swallow it. For example:

AnInterface ai = (AnInterface)base; //and...
((AnInterface)base).InterfaceMethod();

Both statements causes compiler error "CS0175: Use of keyword 'base' is not
valid in this context".

Unlike 'this', which we can refer to as is, C# requires 'base' to be
followed by a period and a member name of the base class. From the on-line
help: "The base (C# Reference) keyword must be used to specify a particular
member of the base class"

One practical solution would be to implement the InterfaceMethod by
declaring it 'public virtual' in the ImplementingClass and 'override' it in
DerivedClass. Then we could use 'base' to access the inherited
implementation of InterfaceMethod. The problem occurs when we don't have the
source code for ImplementingClass. Do you have any ideas?

Regards Carl Johansson
 
Yeah didn't look too closely at that first time around ... d'oh!

The only way I could see is to not use explicit interface implementation
(miss off the "AnInterface." in your interface implementation methods.)

If you must use explicit interface implementation then they are essentially
private methods and can only be accessed through an instance of the
interface.

This might give the effect you're after. I suppose it all depends on what
you need to achieve, but my guess is you're concerned that a derived class
might override an interface implementation and needs to call the base
implementation too. In which case, just don't make the implementation
explicit as illustrated below.

HTH,

Adam
=======

class MainClass
{
static void Main(string[] args)
{
AnInterface ai = new DerivedClass();
ai.InterfaceMethod();
}
}

interface AnInterface
{
void InterfaceMethod();
}

class ImplementingClass : AnInterface
{
public virtual void InterfaceMethod()
{
System.Console.WriteLine("In
ImplementingClass.InterfaceMethod()");
}
}

class DerivedClass : ImplementingClass, AnInterface
{
public override void InterfaceMethod()
{
System.Console.WriteLine("In
DerivedClass.InterfaceMethod()");
base.InterfaceMethod();
}
}
 
Carl Johansson said:
No, unfortunately not! The problem is that 'base' is a reference to
ImplementingClass. What we need is a reference to AnInterface of
ImplementingClass, I guess. I've been trying to cast 'base' in several
ways, but the compiler just won't swallow it. For example:

AnInterface ai = (AnInterface)base; //and...
((AnInterface)base).InterfaceMethod();

Both statements causes compiler error "CS0175: Use of keyword 'base' is
not valid in this context".

Unlike 'this', which we can refer to as is, C# requires 'base' to be
followed by a period and a member name of the base class. From the on-line
help: "The base (C# Reference) keyword must be used to specify a
particular member of the base class"

One practical solution would be to implement the InterfaceMethod by
declaring it 'public virtual' in the ImplementingClass and 'override' it
in DerivedClass. Then we could use 'base' to access the inherited
implementation of InterfaceMethod. The problem occurs when we don't have
the source code for ImplementingClass. Do you have any ideas?

Why do you even bother to implement the interface at the DerivedClass level.
It serves no purpose. Remove the implementation and then you will get what
you want.

PS
 
Yeah didn't look too closely at that first time around ... d'oh!

The only way I could see is to not use explicit interface implementation
(miss off the "AnInterface." in your interface implementation methods.)

If you must use explicit interface implementation then they are essentially
private methods and can only be accessed through an instance of the
interface.

This might give the effect you're after. I suppose it all depends on what
you need to achieve, but my guess is you're concerned that a derived class
might override an interface implementation and needs to call the base
implementation too. In which case, just don't make the implementation
explicit as illustrated below.

HTH,

Adam
=======

class MainClass
{
static void Main(string[] args)
{
AnInterface ai = new DerivedClass();
ai.InterfaceMethod();
}
}

interface AnInterface
{
void InterfaceMethod();
}

class ImplementingClass : AnInterface
{
public virtual void InterfaceMethod()
{
System.Console.WriteLine("In
ImplementingClass.InterfaceMethod()");
}
}

class DerivedClass : ImplementingClass, AnInterface
{
public override void InterfaceMethod()
{
System.Console.WriteLine("In
DerivedClass.InterfaceMethod()");
base.InterfaceMethod();
}
}

===========
Adam.
 
Back
Top