is there a way to add a default constructor to an interface
Asked Answered
E

5

13

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

I've tried:

public interface KadContent<T>
{
    public default KadContent()
    {

    }
...

Getting the error <identifier> expected from Netbeans

Why Needed? I'm using Gson to serialize the objects and getting the "unable to invoke no-args constructor .." error and I do know that I can resolve this problem using Gson's InstanceCreator. But is there a way to create a default Constructor?

Update

I've found the problem with my own code. I was using

gson.fromJson(new String(data), InterfaceName.class);

instead of

gson.fromJson(new String(data), ClassName.class);

So even though the subclass had default constructors, the deserialization code was incorrect. But the question of default constructor still stands.

Eleaseeleatic answered 2/4, 2014 at 12:6 Comment(7)
Interfaces shouldn't have Constructor. They are not part of the object creation. They are supposed to keep the contract between two parties.Discomfortable
What would a default constructor even mean? What would it create an instance of?Piero
Well Gson requires it, that's all the need I have for it.Eleaseeleatic
That does not make sense. I assume you'll need to pass a concrete class to the InstanceCreator instead of the interfaceBioscope
I'm actually trying to write a default method in the interface to handle serialization/deserialization, or should I look at using an abstract class instead for this?Eleaseeleatic
Because, apparently, default constructors cannot be added to interfaces, that means that default methods cannot create new state on an instance, but simply provide utility functionality which can be implemented with the state already exposed by the non-default interface methods...Intertidal
New state on an interface default method can only be accomplished using WeakMaps...Intertidal
U
11

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".

Universalist answered 2/4, 2014 at 12:12 Comment(2)
If java would have real Enums, I would agree with "1.". But you can not extend Enums, and if you want to add some functionality to your own enums then you get stuck with interfaces. And yes: then a default constructor would make sense. The same kind of "it makes no sense" you get when you want to inherit static methods, which makes a lot of sense in some cases.Zoila
The point is that by definition an interface does no provide any functionality. Of course, this has somewhat changed since default methods. But with default methods you cannot change the state of an object directly. The implementation defines what states are possible (by defining member variables), not the interface. The interface just specifies what comes in and what comes out, whereas the implementation details are delegated to the implementation. Thus, it simply does not make sense to provide a default constructor, because you cannot set or change any variables in it.Universalist
T
2

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.

Theological answered 2/4, 2014 at 12:11 Comment(2)
Will default initialize() method be called automatically when this interface class is put on the heap?Murtagh
No, its up to you to call initialize(), e.g in a FactoryTheological
H
2

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.

Habanera answered 2/4, 2014 at 12:16 Comment(0)
S
1

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

Smilacaceous answered 2/4, 2014 at 12:11 Comment(3)
I did that but still getting the error, guess I have to move the serialization code to the class rather than the interfaceEleaseeleatic
depends, if it as an custom serialisation, then the interface could define methos as writeObjectData(DataOutputStream dos) and readObjectData()Theological
Did you remember to make the noArg constructor in your class public?Smilacaceous
S
0

Yes, kind of! You can do:

public interface MyInterface {
  MyInterface NO_OP = new MyInterface() {};
}
Sergias answered 13/12, 2018 at 18:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.