When do we use sealed class
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. When and why would you seal a class? Ask Question. Asked 10 years, 1 month ago. Active 8 months ago. Viewed 44k times. Improve this question. DankMemester 'Andrew Servania' 9 9 silver badges 13 13 bronze badges. Aan Aan 11k 32 32 gold badges 80 80 silver badges bronze badges.
Add a comment. Active Oldest Votes. Improve this answer. AustinWBryan 3, 3 3 gold badges 18 18 silver badges 37 37 bronze badges. Louis Kottmann Louis Kottmann I meant be careful with sealing classes in reused libraries, especially if they're beind reused by third parties and then reintegrated via MEF into the codebase. Your codebase may not inherit a given class but third parties will. The reason 1 sounds vague but, assuming we don't write "security features" most of the time, does that mean reason 1 hardly applies?
Reason 2 is for performance-tuning. How much performance difference are we talking about? Are they significant enough to justify altering a non-security class's definition? Even if the answer would be "yes", this would ideally be a compiler option i. Sealing sucks. It makes testing harder - I would like to mock a couple ASP. Can't agree more with RayLuo. I hit it several times that people sealed their classes where security and performance is not really a problem.
Their "sealed" simply prevented my reasonable need of overriding the classes, made things much more difficult. Like Warlike Chimpanzee said, mocking a class is so common in testing. Show 4 more comments. An addendum to Louis Kottmann's excellent answer : If a class isn't designed for inheritance, subclasses might break class invariants.
This really only applies if you're creating a public API, of course, but as I rule of thumb I seal any class not explicitly designed to be subclassed. And then there's this: Sealing can make unit testing more difficult, as it prohibits mocking. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info.
Contents Exit focus mode. Is this page helpful? Yes No. Any additional feedback? Skip Submit. Submit and view feedback for This product This page. Direct subclasses of sealed classes and interfaces must be declared in the same package.
They may be top-level or nested inside any number of other named classes, named interfaces, or named objects. Subclasses can have any visibility as long as they are compatible with normal inheritance rules in Kotlin. Subclasses of sealed classes must have a proper qualified name. They can't be local nor anonymous objects. These restrictions don't apply to indirect subclasses. If a direct subclass of a sealed class is not marked as sealed, it can be extended in any ways that its modifiers allow:.
There is one more inheritance restriction in multiplatform projects : direct subclasses of sealed classes must reside in the same source set. It applies to sealed classes without the expect and actual modifiers. If a sealed class is declared as expect in a common source set and have actual implementations in platform source sets, both expect and actual versions can have subclasses in their source sets.
0コメント