Why aren't classes sealed by default?

前端 未结 9 1739
天命终不由人
天命终不由人 2020-12-05 22:59

I was just wondering, since the sealed keyword\'s existence indicates that it\'s the class author\'s decision as to whether other classes are allowed to inh

相关标签:
9条回答
  • 2020-12-05 23:58

    Merely deriving from an unsealed class doesn't change the class's behavior. The worst that can happen is that a new version of the base class will add a member with the same name as the deriving class (in which case there will just be a compiler warning saying you should use the new or override modifier) or the base class is sealed (which is a design no-no if the class has already been released into the wild). Arbitrary sublassing still complies with the Liskov Substitution Principle.

    The reason that members are not overridable by default in C# is that because overriding a method can change the base class's behaviour in a way that the base class's author didn't anticipate. By making it explicitly abstract or virtual, it's saying that the author is aware that that it can change or is otherwise beyond their control and the author should have taken this into account.

    0 讨论(0)
  • 2020-12-05 23:59

    For the same reason why objects are not private by default

    or

    to be consistent with the object analogue, which is objects are not private by default

    Just guessing, coz at the end of the day it's a language's design decision and what the creators say is the canon material.

    0 讨论(0)
  • 2020-12-06 00:00

    80% of the features of Word go unused. 80% of classes don't get inherited from. In both cases, once in a while, someone comes along and wants to use or reuse a feature. Why should the original designer prohibit reuse? Let the reuser decide what they want to reuse.

    0 讨论(0)
提交回复
热议问题