473,396 Members | 1,834 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,396 software developers and data experts.

Enforce that a class can only inherit one interface

Hi

Is it possible in one way or another to enforce that a class only inherit
from one of the derived interfaces of a base interface of some kind. For
example.
public class MyClass2 : IMyInterface1 => OK
public class MyClass2 : IMyInterface1, IMyInterface2 => Error

Regards
/Niklas

public interface IMyInterfacsBase
{
string MyMethod1();
}

public interface IMyInterface1 : IMyInterfacsBase
{
int MyInt1{ get; }
}

public interface IMyInterface2 : IMyInterfacsBase
{
int MyInt1{ get;}
int MyInt2{ get;}
}

public class MyClass2 : IMyInterface1, IMyInterface2
{
private int _MyInt1 = 4;
private int _MyInt2 = 5;

public int MyInt1
{
get { return _MyInt1; }
}

public int MyInt2
{
get { return _MyInt2; }
}

public string MyMethod1()
{
throw new NotImplementedException();
}
}
Feb 22 '06 #1
2 1861
Hi,

"Niklas" <Ni****@discussions.microsoft.com> wrote in message
news:5A**********************************@microsof t.com...
Hi

Is it possible in one way or another to enforce that a class only inherit
from one of the derived interfaces of a base interface of some kind. For


First of all, a class do not inherit an interface, it implement it. which is
different.

And no, there is no way I know of to especify that a class can implement at
the most one nterface.

If you substitute interface by class you can be sure of that, as C# ins a
single inheritance language.

why you want to do this anyway?

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Feb 22 '06 #2
"Niklas" <Ni****@discussions.microsoft.com> a écrit dans le message de news:
5A**********************************@microsoft.com...

| Is it possible in one way or another to enforce that a class only inherit
| from one of the derived interfaces of a base interface of some kind. For
| example.
| public class MyClass2 : IMyInterface1 => OK
| public class MyClass2 : IMyInterface1, IMyInterface2 => Error

No, but you can delegate an interface to an aggregated object inside the
main class, so that each interface's behaviour is looked after separately,
even though the class implements more than one interface.

What are you really trying to achieve ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must...
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...
1
by: HC | last post by:
I've stripped down my code to barebones to demonstrate (below). I have interfaces and classes which implement them. I force my generic collection to accept only the interface type. If I create an...
3
by: Matt F. | last post by:
I have an abstract class that about a dozen sub-classes inherit from. I want to enforce that each sub-class shadows an event in the abstract class, but can't quite figure out how to do this. ...
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.