Why are interfaces preferred to abstract classes?

前端 未结 23 1438
鱼传尺愫
鱼传尺愫 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:15

    "Why Interfaces are preferred over Abstract classes?"

    The other posts have done a great job of looking at the differences between interfaces and abstract classes, so I won't duplicate those thoughts.

    But looking at the interview question, the better question is really "When should interfaces be preferred over abstract classes?" (and vice versa).

    As with most programming constructs, they're available for a reason and absolute statements like the one in the interview question tend to miss that. It sort of reminds me of all the statement you used to read regarding the goto statement in C. "You should never use goto - it reveals poor coding skills." However, goto always had its appropriate uses.

提交回复
热议问题