Can this be implemented?
Hello, I'd like to implement an interface that meets the following conditions. Is it possible?
14 Replies
Interfaces cannot inherit classes, so no
Classes can implement interfaces
Interfaces can implement other interfaces
Classes can inherit from other classes
I misinterpreted. So, is this possible?
There are class A, interfaces B, C, and D
B can only inherit A or objects that inherit A. In other words, classes that do not inherit A cannot inherit B.
C can't be inherited like D. In other words, it's impossible to inherit C and D together in any class.
Again, interfaces cannot inherit classes
This is what you can do
Additionally, a class can implement many interfaces
And an interface can implement many interfaces
But a class can only ever inherit from one class
it's impossible to inherit C and D together in any classu can not have such constraints with interfaces if u want such constraints u have to fiddle around to get a specific class hierarchy, as only with classes u can somewhat limit inheritance like that whats the actual use case here?
You could do this using a generic constraint, but again what's the use-case?
u cant constrain with generics either that something does not implement an interface or am i missing something here?
If you wanted an interface which implementing classes have to inherit another class you could do
interface IA<T> where T : B, IA<T>
, and if B
doesn't inherit anything else then you're indirectly restricting the inheritance chain.
But this is less useful as you have to use generics to pass instances of the interfacei was actually talking about the "can not implement both interfaces at the same time" thingy
ah
yeah can't do that
Okay, okay, I think the translator is weird, so I'll just give you an example and explain it
Can I do like that?
its impossible to disallow a class to implement a certain interface
But what you're trying to do is really unnecessary and hard to achieve, drop it
What would even be the use case for that