However, the library doesn't know about errors declared outside it, so it can't treat them consistently with its own classes. If the hierarchy of such error classes includes interfaces or abstract classes visible in the public API, then nothing prevents implementing or extending them in the client code. It's likely to contain error classes to let the library users handle errors that it can throw. In some sense, sealed classes are similar to enum classes: the set of values for an enum type is also restricted, but each enum constant exists only as a single instance, whereas a subclass of a sealed class can have multiple instances, each with its own state.Īs an example, consider a library's API. The same works for sealed interfaces and their implementations: once a module with a sealed interface is compiled, no new implementations can appear. Thus, each instance of a sealed class has a type from a limited set that is known when this class is compiled. For example, third-party clients can't extend your sealed class in their code. No other subclasses may appear outside a module within which the sealed class is defined. All direct subclasses of a sealed class are known at compile time. Sealed classes and interfaces represent restricted class hierarchies that provide more control over inheritance.
0 Comments
Leave a Reply. |