Is there any point in any programmer's lifetime to restrict a class to extending this class?
Example is that I don't want Package
to extend Box
.
Asked
Active
Viewed 168 times
0

Mark Vizcarra
- 1,165
- 1
- 11
- 15
-
are you asking if you can stop box from being extended or stop package from extending box while still allowing other things to extend box? – Keith Nicholas Aug 29 '13 at 00:36
-
Stop Package or any class to extend Box – Mark Vizcarra Aug 29 '13 at 00:41
1 Answers
0
Yes. In Java they call this a final
class. In C# they call it a sealed
class.
It is done because you cannot always predict the many ways a consumer might extend your class, so to prevent you from having to design your class to account for all possible ways someone might extend it, you simply seal it and require the consumer to use composition instead of inheritance.
Further Reading
Why Are So Many Of The Framework Classes Sealed?

Robert Harvey
- 178,213
- 47
- 333
- 501