Benefits of using an abstract classes vs. regular class

后端 未结 9 916
独厮守ぢ
独厮守ぢ 2021-01-31 17:52

I have decided to start doing small coding projects on my own that focus on code quality instead of code quantity and have a question about the use of abstract classes.

9条回答
  •  臣服心动
    2021-01-31 18:29

    In addition to not being able to create instances of abstract classes, some languages may support having abstract methods in abstract classes - similar to interfaces, an abstract method will have to be implemented by the class inheriting from the abstract class.

    The main benefit of abstract classes in my opinion is if there is some code that has to be shared between classes of the same type. Usually you could use an interface for this, but sometimes the functionality of such classes may overlap and you would end up with code duplication. In this case you can use an abstract class and just put the code there.

提交回复
热议问题