what is the advantage of interface over abstract classes?

前端 未结 9 1883
野的像风
野的像风 2021-02-01 05:48

In Java, abstract classes give the ability to define both concrete and abstract methods whereas interfaces only give the ability to implement abstract methods. I believe overrid

相关标签:
9条回答
  • 2021-02-01 06:25

    -Method without any implementation is abstract method,whenever a class contains one or more abstract method,then it must be declared as a abstract class

    -Interface is fully abstract which cannot have constructor,instance and static blocks,and it contains only two types of members 1.public abstract method 2.public-static-final variable

    *Both cannot be instantiated but reference can be created.

    *Which one suits better depends on the application -Interfaces are useful because Java classes will not support multiple inheritance but interfaces do.

    -Abstract classes are useful when you need concrete behavior from the base class.

    0 讨论(0)
  • 2021-02-01 06:27

    An class may implement several interfaces, whereas it may only extend one class (abstract or concrete), because Java does not support multiple inheritance.

    0 讨论(0)
  • 2021-02-01 06:30

    Interfaces are useful because Java doesn't have multiple inheritance (but you can implement as many interfaces as you like).

    Abstract classes are useful when you need concrete behaviour from the base class.

    0 讨论(0)
  • 2021-02-01 06:31

    In OOP (mostly independent of a concrete language) abstract classes are a re-use mechanism for the class hierarchy for behaviour and structure which isn't complete on its own. Interfaces are mechanism for specification of requirements on a module (e.g. class) independently of the concrete implementation. All other differences are technical details, important is different usage.

    0 讨论(0)
  • 2021-02-01 06:33

    The facts are-

    1. Java doesn't support multiple inheritance
    2. Multiple interfaces can be implemented
    3. Few methods in an abstract class may be implemented

    These facts can be used to tilt the advantage in favor of interfaces or abstract classes.

    If there are more than one behavior that a class must share with other classes, interfaces win. If a method definition has to be shared/ overridden with other classes, abstract classes win.

    0 讨论(0)
  • 2021-02-01 06:33

    You dont override an interface. You implement it.

    Writing an interface gives implementors the ability to implement your interface and also other interfaces in addition to inheriting from a base class.

    Abstract classes can be partially or fully implemented.Marking a class abstract just prevents you from instantiating an object of that type.

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