Question

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.

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top