When to use: Java 8+ interface default method, vs. abstract method

后端 未结 15 1759
闹比i
闹比i 2020-11-22 07:01

Java 8 allows for default implementation of methods in interfaces called Default Methods.

I am confused between when would I use that sort of interface default

相关标签:
15条回答
  • 2020-11-22 07:19

    Although its an old question let me give my input on it as well.

    1. abstract class: Inside abstract class we can declare instance variables, which are required to the child class

      Interface: Inside interface every variables is always public static and final we cannot declare instance variables

    2. abstract class: Abstract class can talk about state of object

      Interface: Interface can never talk about state of object

    3. abstract class: Inside Abstract class we can declare constructors

      Interface: Inside interface we cannot declare constructors as purpose of
      constructors is to initialize instance variables. So what is the need of constructor there if we cannot have instance variables in interfaces.

    4. abstract class: Inside abstract class we can declare instance and static blocks

      Interface: Interfaces cannot have instance and static blocks.

    5. abstract class: Abstract class cannot refer lambda expression

      Interfaces: Interfaces with single abstract method can refer lambda expression

    6. abstract class: Inside abstract class we can override OBJECT CLASS methods

      Interfaces: We cannot override OBJECT CLASS methods inside interfaces.

    I will end on the note that:

    Default method concepts/static method concepts in interface came just to save implementation classes but not to provide meaningful useful implementation. Default methods/static methods are kind of dummy implementation, "if you want you can use them or you can override them (in case of default methods) in implementation class" Thus saving us from implementing new methods in implementation classes whenever new methods in interfaces are added. Therefore interfaces can never be equal to abstract classes.

    0 讨论(0)
  • 2020-11-22 07:21

    In Java 8, an interface looks like an abstract class although their might be some differences such as :

    1) Abstract classes are classes, so they are not restricted to other restrictions of the interface in Java e.g. abstract class can have the state, but you cannot have the state on the interface in Java.

    2) Another semantic difference between interface with default methods and abstract class is that you can define constructors inside an abstract class, but you cannot define constructor inside interface in Java

    0 讨论(0)
  • 2020-11-22 07:21

    Please think first of open/closed principle. The default methods in interfaces DO VIOLATE it. This is a bad feature in Java. It encourages bad design, bad architecture, low software quality. I would suggest to avoid using default methods completely.

    Ask yourself a few questions: Why can't you put your methods to the abstract class? Would you need then more than one abstract class? Then think about what is your class responsible for. Are you sure all methods you are going to put to the single class really fulfill the same purpose? May be you will distinguish several purposes and will then split your class into several classes, for each purpose its own class.

    0 讨论(0)
  • 2020-11-22 07:22

    Regarding your query of

    So when should interface with default methods be used and when should an abstract class be used? Are the abstract classes still useful in that scenario?

    java documentation provides perfect answer.

    Abstract Classes Compared to Interfaces:

    Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation.

    However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.

    With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.

    Use cases for each of them have been explained in below SE post:

    What is the difference between an interface and abstract class?

    Are the abstract classes still useful in that scenario?

    Yes. They are still useful. They can contain non-static, non-final methods and attributes (protected, private in addition to public), which is not possible even with Java-8 interfaces.

    0 讨论(0)
  • 2020-11-22 07:28

    Default methods in Java Interface are to be used more for providing dummy implementation of a function thus saving any implementing class of that interface from the pain of declaring all the abstract methods even if they want to deal with only one. Default methods in interface are thus in a way more a replacement for the concept of adapter classes.

    The methods in abstract class are however supposed to give a meaningful implementation which any child class should override only if needed to override a common functionality.

    0 讨论(0)
  • 2020-11-22 07:33

    There's a lot more to abstract classes than default method implementations (such as private state), but as of Java 8, whenever you have the choice of either, you should go with the defender (aka. default) method in the interface.

    The constraint on the default method is that it can be implemented only in the terms of calls to other interface methods, with no reference to a particular implementation's state. So the main use case is higher-level and convenience methods.

    The good thing about this new feature is that, where before you were forced to use an abstract class for the convenience methods, thus constraining the implementor to single inheritance, now you can have a really clean design with just the interface and a minimum of implementation effort forced on the programmer.

    The original motivation to introduce default methods to Java 8 was the desire to extend the Collections Framework interfaces with lambda-oriented methods without breaking any existing implementations. Although this is more relevant to the authors of public libraries, you may find the same feature useful in your project as well. You've got one centralized place where to add new convenience and you don't have to rely on how the rest of the type hierarchy looks.

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