Combined interface inheritance

D

Donal McWeeney

I was under the impression that in an interface declaration I could inherit
another interface declaration and the result would be that the inheriting
interface could include the methods and properties defined in the inherited
interface.

However when playing with this and defining a class to implement the
interface the class gets implemented with both sets of interface
declarations.

eg.

interface A
{
string myPropA { get ; }
}

interface B
{
string myPropB { get ; }
}

what I though is that the interface B now includes the myPropA property and
when you go to implement it in a class you could namespace it with the
interface name...

class Z : B
{
string B.myPropA {... } *** fails

string B.myPropB {... }
}

Is it possible to do what I want to do?

Thanks

Donal
 
B

Bruno Jouhier [MVP]

You forgot to say that interface B extends interface A:

interface B : A
{
string myPropB { get; }
}

Bruno
 
N

n!

what I though is that the interface B now includes the myPropA property
and
when you go to implement it in a class you could namespace it with the
interface name...

class Z : B
{
string B.myPropA {... } *** fails

string B.myPropB {... }
}

You're not 'namespacing' the implementation here. This is called 'explicit
interface implementation', the reason the compiler fails is that myPropA is
explicitly a member of interface A, not an explicit member of interface B.

The following should compile:

class Z : B
{
string A.myPropA {... } *** compiles

string B.myPropB {... }
}

Unless you have a reason for using explicit interface implementation I'd
recommend not doing it, explicitly implemented methods are only accessible
through an interface instance rather than via the class itself. Removing
explicit implementation from the above code leaves you with the following:

class Z : B
{
public string myPropA { }
public string myPropB { }
}

Which is the more frequently used method of implementing interfaces.

n!
 
D

Donal McWeeney

Hi,

The reason I am doing it this was is because I want to reuse the interface
definition A, among other interfaces... eg.

interface A { ... }
interface B : A { ... }
interface C : A { ... }
interface D : A { ... }
etc

and a number of classes would implement these interfaces.

class One : B, C, D { ... }
class Two : B, C, D { ... }
class Three : B, C, D { ... }

Which is the reason I am explitily implementing the interfaces...

Thanks

Donal
 
N

n!

interface A { ... }
interface B : A { ... }
interface C : A { ... }
interface D : A { ... }
etc

and a number of classes would implement these interfaces.

class One : B, C, D { ... }
class Two : B, C, D { ... }
class Three : B, C, D { ... }

Which is the reason I am explitily implementing the interfaces...

That looks like you're trying to emulate some kind of multiple inheritance?
AFAIK if you inherit B,C,D and they all inherit A, then your class only has
one A rather than three (though I could be wrong on that count). The
heirarchy looks overly complicated, and I'd suggest there must be a better
layout for such a requirement. Perhaps using containment rather than
inheritance?

n!
 
D

Donal McWeeney

Thanks for the advise...

n! said:
That looks like you're trying to emulate some kind of multiple inheritance?
AFAIK if you inherit B,C,D and they all inherit A, then your class only has
one A rather than three (though I could be wrong on that count). The
heirarchy looks overly complicated, and I'd suggest there must be a better
layout for such a requirement. Perhaps using containment rather than
inheritance?

n!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top