What's the difference between an abstract class and a static one?

后端 未结 9 1908
臣服心动
臣服心动 2020-12-02 18:55

Neither is instantiable. What are the differences, and in what situations might you use one or the other?

9条回答
  •  攒了一身酷
    2020-12-02 19:20

    An abstract class is intended to be used as a base of a class inheritance hierarchy. A static class cannot be the base of a class inheritance hierarchy.

    A static class is intended for singleton state or stateless functionality. An abstract class is not suitable for singleton functionality, because, even though it may contain static methods and fields as a static class does, it cannot forbid inheritance, so the singleton use may be defeated by subclasses. Or, at the very least, it would be confusing to other programmers, because its definition would communicate an intent that is different from its actual intended use.

    The superficial similarity between abstract and static classes is only in the fact that neither may be instantiated. Beyond that, they are completely different animals with completely different use cases.

提交回复
热议问题