Why are interfaces preferred to abstract classes?

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

    As devinb and others mention, it sounds like the interviewer shows their ignorance in not accepting your valid answers.

    However, the mention of JDBC might be a hint. In that case, perhaps they are asking for the benefits of a client coding against an interface instead of a class.

    So instead of perfectly valid answers such as "you only get one use of inheritance", which are relating to class design, they may be looking for an answer more like "decouples a client from a specific implementation".

提交回复
热议问题