is there a way to add a default constructor to an interface

前端 未结 5 1171
梦如初夏
梦如初夏 2021-02-14 12:26

With default methods now added to Java 8, is there any way to create a default constructor?

I\'ve tried:

public interface KadContent
{
    publ         


        
相关标签:
5条回答
  • 2021-02-14 12:46

    Yes, kind of! You can do:

    public interface MyInterface {
      MyInterface NO_OP = new MyInterface() {};
    }
    
    0 讨论(0)
  • 2021-02-14 13:00

    Constructors are when the objects come into picture and the fact that a object for an interface cannot be constructed is SOUND, be it Java, C# or Java8

    So... if you have any functionality that you would want to define by default in the interface level, Java8 introduces the concept of Default Methods.

    0 讨论(0)
  • 2021-02-14 13:07

    You need to add the default constructor to the class you want to serialize.

    0 讨论(0)
  • 2021-02-14 13:08

    It does not make sense to provide an Constructor in an Interface.

    Check if it makes sense for you to provide a default initialize() method instead.

    0 讨论(0)
  • 2021-02-14 13:12

    No, this is not possible.

    1. It does not make sense in an interface
    2. If you implement an interface, the class has already a default constructor (the one without arguments)

    You may want to use an abstract class if you want implementations have a "default constructor".

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