What is the main advantage of making a class abstract

前端 未结 9 572
渐次进展
渐次进展 2020-12-21 06:47

Why do we declare a class as abstract? I know it cannot be instantiated, but why give a special keyword for it. Even a \'normal\' class will work just as well and can be eas

9条回答
  •  误落风尘
    2020-12-21 07:34

    Declaring the class abstract prevents any code from instantiating the class.

    This enforces the design guideline to make non-leaf classes abstract.

    It allows you to add abstract methods to your superclass (and implementations to the subclasses) later, without affecting any existing clients.

    The abstract keyword works even if the non-leaf class does not currently have any abstract methods.

提交回复
热议问题