473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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 5181
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tom Evans | last post by:
My basic question: If I have a specific interface which I know is going to be implemented by a number of classes, but there is no implementation commonality between them, what is the preferred...
4
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by...
4
by: christopher diggins | last post by:
A feature that I find signficantly missing in C# is the ability to write functions in interfaces that can call other functions of the interface. Given an interface ISomeInteface the only way we can...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
7
by: Hazz | last post by:
Are there any good references/articles/books which provide clarity toward my insecurity still on deciding how to model a complex system? I still feel uncomfortable with my understanding, even...
10
by: Brett | last post by:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract...
6
by: John Salerno | last post by:
I understand how they work (basically), but I think maybe the examples I'm reading are too elementary to really show their value. Here's one from Programming C#: #region Using directives ...
12
by: Meya-awe | last post by:
I am puzzled, what is the purpose of an interface? How does it work, what i mean is how does the compiler treats this? Why when we talk about separating user interface from business logic, an...
4
by: Raja Chandrasekaran | last post by:
Hai friends, I really wonder, If the interface does not have any definition, Y do we need to use interface. You can then only we can use Multiple inheritance. I really cant understand, Just for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.