What is the main advantage of making a class abstract

前端 未结 9 552
渐次进展
渐次进展 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

    It's useful if you want to have a group of classes that inherit the same logical functions. But in the same time, the class only implements the basic logic, and doesn't contain any real functionality.

    You should see it as a skeleton class.

    For example, I once made a class with these specifications :

    1. Controls the process of executing a command on another thread, and relays events of that process.

    2. The class itself didn't have any functionality by itself (didn't implement the actual work() function)

    So the result is an abstract class, that can be inherited, that already has a built in thread control, which all you need to do is just implement the work() method.

提交回复
热议问题