Why are interfaces preferred to abstract classes?

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

    You only get one shot at inheritance. If you make an abstract class rather than an interface, someone who inherits your class can't also inherit a different abstract class.

    0 讨论(0)
  • 2020-12-02 07:14

    interface is not substitute for abstract class.

    Prefer

    interface: To implement a contract by multiple unrelated objects

    abstract class: To implement the same or different behaviour among multiple related objects


    Refer to this related SE question for use cases of both interface and abstract class

    Interface vs Abstract Class (general OO)

    Use case:

    If you have to use Template_method pattern, you can't achieve with interface. Abstract class should be chosen to achieve it.

    If you have to implement a capability for many unrleated objects, abstract class does not serve the purpose and you have to chose interface.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-02 07:15

    If they think that X is better than Y I wouldn't be worried about getting the job, I wouldn't like working for someone who forced me to one design over another because they were told interfaces are the best. Both are good depending on the situation, otherwise why did the language choose to add abstract classes? Surely, the language designers are smarter than me.

    0 讨论(0)
  • 2020-12-02 07:17

    You define interfaces when you only require that some object implement certain methods but you don't care about its pedigree. So someone can extend an existing class to implement an interface, without affecting the previously existing behavior of that class.

    That's why JDBC is all interfaces; you don't really care what classes are used in a JDBC implementation, you only need any JDBC implementation to have the same expected behavior. Internally, the Oracle JDBC driver may be very different from the PostgreSQL driver, but that's irrelevant to you. One may have to inherit from some internal classes that the database developers already had, while another one may be completely developed from scratch, but that's not important to you as long as they both implement the same interfaces so that you can communicate with one or the other without knowing the internal workings of either.

    0 讨论(0)
  • 2020-12-02 07:17

    Well, I'd suggest the question itself should be rephrased. Interfaces are mainly contracts that a class acquires, the implementation of that contract itself will vary. An abstract class will usually contain some default logic and its child classes will add some more logic. I'd say that the answer to the questions relies on the diamond problem. Java prevents multiple inheritance to avoid it. ( http://en.wikipedia.org/wiki/Diamond_problem ).

    0 讨论(0)
提交回复
热议问题