C
C#2y ago
Hugh

❔ ✅ Stopping any additional subclassing of a class

I've got the following code:
public abstract class MyBase {}

public class MyType1 : MyBase {}
public class MyType2 : MyBase {}
public abstract class MyBase {}

public class MyType1 : MyBase {}
public class MyType2 : MyBase {}
Is there a way of stopping any other classes subclassing MyBase other than the two here? I need to be able to do these subclasses, but don't want anyone to be able to do any more.
24 Replies
Aaron
Aaron2y ago
kind of? none of them are very good though you can put all 3 types in a different assembly, with the constructors of MyBase all being internal, which means only types in that assembly can be subclasses or you can have the two derived classes be nested inside the base class, with a private constructor
Pobiega
Pobiega2y ago
Yeah, access modifiers is the way to go here. but you should ask yourself why you need this and make sure this is a hard requirement, as this will break the extensbility of the system
Aaron
Aaron2y ago
actually, if you mean "anyone else" as in users of like a library you're writing, then yeah, just making the constructors of MyBase internal will do it idk how I read that differently
Hugh
Hugh2y ago
making the constructors internal sounds like it will do the job. In this case, I need all user subclasses to come from MyType1 and MyType2. However, there are some functions that will accept any subclass of MyBase And yes, I'm confident that this is a hard requirement
Pobiega
Pobiega2y ago
can you clarify what you mean by "anyone else" thou? Will this be a library, or do you mean other developers in the same codebase?
Hugh
Hugh2y ago
This is in a library that is used by other applications
Pobiega
Pobiega2y ago
right. then just make MyBase internal and you are good
Hugh
Hugh2y ago
"anyone else" could well include future me Perfect - thanks - appreciate it
FusedQyou
FusedQyou2y ago
You make them internal. You can still inherit from the same assembly, but then it's something you can control yourself
Hugh
Hugh2y ago
If I make MyBase internal, then I can't make MyType public, which I need Making the constructor internal certainly worked well though
FusedQyou
FusedQyou2y ago
Alternatively you make the accessibility File, assuming you're in .NET 7, and put both classes in the same file 🙃
jalepi
jalepi2y ago
Prefer composition over inheritance for better modularity and therefore better encapsulation and avoid further inheritance. Make your classes sealed, implementing a public interface for the consumers of your library.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
jalepi
jalepi2y ago
Does it prevent classes being derived?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
jalepi
jalepi2y ago
It works, also prevents the classes to be instantiated directly from outside the assembly. 👌
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
$U₹¥Δ
$U₹¥Δ2y ago
mark the class with sealed keyword
Aaron
Aaron2y ago
that stops further derivation of the derived, not more derived classes of the base
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Hugh
Hugh2y ago
I tried to close it a few times but the bot was down. I’ll try again now There we go
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Hugh
Hugh2y ago
Oops Closing again now Thanks
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.