Domanda

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.

È stato utile?

Soluzione

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?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top