469,572 Members | 1,220 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

Combined interface inheritance

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
Nov 15 '05 #1
6 5058
You forgot to say that interface B extends interface A:

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

Bruno

"Donal McWeeney" <do************@NO-SP-AM.aimware.com> a écrit dans le
message de news:OL**************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #2
I did, thanks... any ideas of how I should tackle this.

"Bruno Jouhier [MVP]" <bj******@club-internet.fr> wrote in message
news:eO**************@TK2MSFTNGP10.phx.gbl...
You forgot to say that interface B extends interface A:

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

Bruno

"Donal McWeeney" <do************@NO-SP-AM.aimware.com> a écrit dans le
message de news:OL**************@TK2MSFTNGP10.phx.gbl...
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


Nov 15 '05 #3
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!
Nov 15 '05 #4
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!" <nf********@nomailplease.com> wrote in message
news:uE**************@TK2MSFTNGP12.phx.gbl...
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!

Nov 15 '05 #5
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!
Nov 15 '05 #6
Thanks for the advise...

"n!" <nf********@nomailplease.com> wrote in message
news:ud**************@TK2MSFTNGP10.phx.gbl...
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!

Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Tom Evans | last post: by
4 posts views Thread by Roy Pereira | last post: by
4 posts views Thread by christopher diggins | last post: by
21 posts views Thread by Helge Jensen | last post: by
7 posts views Thread by Hazz | last post: by
10 posts views Thread by Brett | last post: by
6 posts views Thread by John Salerno | last post: by
12 posts views Thread by Meya-awe | last post: by
4 posts views Thread by Raja Chandrasekaran | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.