C
C#11mo ago
! 휘백

Can this be implemented?

Hello, I'd like to implement an interface that meets the following conditions. Is it possible?
1. that interface can inherited class that inherit class A.
2. that interface cannot inherited class that inherit interface B.
1. that interface can inherited class that inherit class A.
2. that interface cannot inherited class that inherit interface B.
14 Replies
Angius
Angius11mo ago
Interfaces cannot inherit classes, so no Classes can implement interfaces Interfaces can implement other interfaces Classes can inherit from other classes
class Foo : IFoo {} // yes
class Foo : Bar {} // yes
interface IFoo : IBar {} // yes
interface IFoo : Foo {} // no
class Foo : IFoo {} // yes
class Foo : Bar {} // yes
interface IFoo : IBar {} // yes
interface IFoo : Foo {} // no
! 휘백
! 휘백OP11mo ago
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.
Angius
Angius11mo ago
Again, interfaces cannot inherit classes
interface IBar {}
class Foo : IBar {} // class Foo implements interface IBar
interface IBar {}
class Foo : IBar {} // class Foo implements interface IBar
class Foo {}
class Bar : Foo {} // class Bar inherits from class Foo
class Foo {}
class Bar : Foo {} // class Bar inherits from class Foo
interface IFoo {}
interface IBar : IFoo {} // interface IBar implements interface IFoo
interface IFoo {}
interface IBar : IFoo {} // interface IBar implements interface IFoo
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
cap5lut
cap5lut11mo ago
it's impossible to inherit C and D together in any class
u 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?
Thinker
Thinker11mo ago
You could do this using a generic constraint, but again what's the use-case?
cap5lut
cap5lut11mo ago
u cant constrain with generics either that something does not implement an interface or am i missing something here?
Thinker
Thinker11mo ago
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 interface
cap5lut
cap5lut11mo ago
i was actually talking about the "can not implement both interfaces at the same time" thingy
Thinker
Thinker11mo ago
ah yeah can't do that
! 휘백
! 휘백OP11mo ago
Okay, okay, I think the translator is weird, so I'll just give you an example and explain it
class A {}

interface B {}
interface C {}
interface D {}
...
class SA : A, B {} //I want it work
class OT : O, B {} //I want it don't work

class SC : C {} //Work
class SD : D {} //Work
class SCD : C, D {} //Don't work
class A {}

interface B {}
interface C {}
interface D {}
...
class SA : A, B {} //I want it work
class OT : O, B {} //I want it don't work

class SC : C {} //Work
class SD : D {} //Work
class SCD : C, D {} //Don't work
Can I do like that?
cap5lut
cap5lut11mo ago
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
Gax
Gax11mo ago
What would even be the use case for that
Want results from more Discord servers?
Add your server