Should an abstract class have at least one abstract method?

前端 未结 5 2104
眼角桃花
眼角桃花 2021-02-02 12:17

Is it necessary for an abstract class to have at least one abstract method?

5条回答
  •  抹茶落季
    2021-02-02 13:06

    The subject of this post and the body ask two different questions:

    1. Should it have at least one abstract member?
    2. Is it necessary to have at least one abstract member?

    The answer to #2 is definitively no.

    The answer to #1 is subjective and a matter of style. Personally I would say yes. If your intent is to prevent a class (with no abstract methods) from being instantiated, the best way to handle this is with a privateprotected constructor, not by marking it abstract.

提交回复
热议问题