Assume the following: we have class B, which is a private class nested inside class A. There isn't any class inheriting from class B. The question is: will the compiler automatically mark class B as Sealed? (NonInheritable in VB). Is there any good reason for the compiler not to mark class B as sealed?
My line of thought is this: since class B is nested inside class A and is private, and there is no other class inheriting from class B, it should be safe to seal it, because it can't be inherited outside class A (not even by subclasses of A).
Class A
Private Class B
End Class
End Class
Sealed classes can be declared directly inside the namespace. We cannot create an instance of a private class.
Marking a class as Sealed prevents tampering of important classes that can compromise security, or affect performance. Many times, sealing a class also makes sense when one is designing a utility class with fixed behaviour, which we don't want to change.
A Private class can only be accessed by the class it is defined and contain within - it is completely inaccessible to outside classes. A Sealed class can be accessed by any class, but can not be derived from.
Techopedia Explains Sealed Class A sealed class can be useful only if it has methods with public-level accessibility. A sealed class cannot be an abstract class as the abstract class is intended to be derived by another class that provides implementation for the abstract methods and properties.
The compiler will not automatically mark this type as sealed.
True in this very specific scenario there is no real value in leaving the class as unsealed. However determining that you are in this scenario is not always so easy. You have to consider the following
These are not impossible to calculate but it's not trivial either. It's much cheaper to ask the user to just seal the type themselves
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With