Pregunta

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.

¿Fue útil?

Solución

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?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top