Why should constructors on abstract classes be protected, not public?

前端 未结 3 1909
礼貌的吻别
礼貌的吻别 2020-12-24 10:52

ReSharper suggests changing the accessibility of a public constructor in an abstract class to protected, but it does not state the rat

3条回答
  •  旧时难觅i
    2020-12-24 11:32

    Simply because being public makes no sense in an abstract class. An abstract class by definition cannot be instantiated directly. It can only be instantiated by an instance of a derived type. Therefore the only types that should have access to a constructor are its derived types and hence protected makes much more sense than public. It more accurately describes the accessibility.

提交回复
热议问题