Why are interfaces preferred to abstract classes?

前端 未结 23 1440
鱼传尺愫
鱼传尺愫 2020-12-02 06:23

I recently attended an interview and they asked me the question \"Why Interfaces are preferred over Abstract classes?\"

I tried giving a few answers like:

23条回答
  •  渐次进展
    2020-12-02 07:24

    When you use abstract classes you create a coupling between the subclass and the base class. This coupling can sometimes make code really hard to change, especially as the number of subclasses increases. Interfaces do not have this problem.

    You also only have one inheritance, so you should make sure you use it for the proper reasons.

提交回复
热议问题