Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should you prevent a class from being subclassed?

Tags:

oop

What can be reasons to prevent a class from being inherited? (e.g. using sealed on a c# class) Right now I can't think of any.

like image 827
FantaMango77 Avatar asked Aug 23 '08 21:08

FantaMango77


1 Answers

Because writing classes to be substitutably extended is damn hard and requires you to make accurate predictions of how future users will want to extend what you've written.

Sealing your class forces them to use composition, which is much more robust.

like image 95
DrPizza Avatar answered Oct 23 '22 18:10

DrPizza