If I have 3 classes as follows:
abstract class A {}
public class B : A {}
public class C: A {}
I want to force other code to use B or C directly, or inherit from either B
or C
. I don't want other code to inherit directly from A
.
Is this even possible?
A
cannot be private since B
and C
must be public, making A private would cause "Inconsistent accessibility". A
can't be internal
for the same reason.
Why do I want this?
I have designed an API, the response of which must include either property x, or both properties y & z, but not all 3. It also includes a bunch of other properties that are common to both possibilities. i.e. -
{
"allOf": [
{
"type": "object",
"properties": {
"w": { ... }
}
},
{
"anyOf": [
{
"type": "object",
"properties": {
"x": { ... }
}
},
{
"type": "object",
"properties": {
"y": { ... },
"z": { ... }
}
}
]
}
]
}