Why are interfaces preferred to abstract classes?

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

    Abstract classes are used when you inherit implementation, interfaces are used when you inherit specification. The JDBC standards state that "A connection must do this". That's specification.

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

    interfaces are a cleaner way of writing a purely abstract class. You can tell that implementation has not sneaked in (of course you might want to do that at certain maintenance stages, which makes interfaces bad). That's about it. There is almost no difference discernible to client code.

    JDBC is a really bad example. Ask anyone who has tried to implement the interfaces and maintain the code between JDK releases. JAX-WS is even worse, adding methods in update releases.

    There are technical differences, such as the ability to multiply "inherit" interface. That tends to be the result of confused design. In rare cases it might be useful to have an implementation hierarchy that is different from the interface hierarchy.

    On the downside for interfaces, the compiler is unable to pick up on some impossible casts/instanceofs.

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

    Abstract Classes

    1.Cannot be instantiated independently from their derived classes. Abstract class constructors are called only by their derived classes.

    2.Define abstract member signatures that base classes must implement.

    3.Are more extensible than interfaces, without breaking any version compatibility. With abstract classes, it is possible to add additional nonabstract members that all derived classes can inherit.

    4.Can include data stored in fields.

    5.Allow for (virtual) members that have implementation and, therefore, provide a default implementation of a member to the deriving class.

    6.Deriving from an abstract class uses up a subclass's one and only base class option.

    Interface

    1.Cannot be instantiated.

    2.Implementation of all members of the interface occurs in the base class. It is not possible to implement only some members within the implementing class.

    3.Extending interfaces with additional members breaks the version compatibility.

    4.Cannot store any data. Fields can be specified only on the deriving classes. The workaround for this is to define properties, but without implementation.

    5.All members are automatically virtual and cannot include any implementation.

    6.Although no default implementation can appear, classes implementing interfaces can continue to derive from one another.

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

    This is the issue of "Multiple Inheritance". We can "extends" not more than one abstarct class at one time through another class but in Interfaces, we can "implement" multiple interfaces in single class. So, though Java doesn't provide multiple inheritance in general but by using interfaces we can incorporate multiplt inheritance property in it.

    Hope this helps!!!

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

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