2

I read this question. The answer says that even in Java 8 (where we can have default methods in interfaces), we cannot have default constructors. And it says that it makes no sense.

Can someone explain why it doesn't make any sense or whatever the reason there is no support for default constructors in Java 8?

Community
  • 1
  • 1
Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
  • 3
    Why would you want to be able to instantiate an interface? – Michael Markidis Jun 30 '16 at 05:18
  • 4
    constructors typically set up member variables, which an interface doesn't have. so what would this default constructor typically do? also, a class can implement multiple interfaces - so which default constructor would your class inherit? – slipperyseal Jun 30 '16 at 05:20
  • Even not for that interface, it can be inherited by concrete child objects, same as an abstract class's constructor. – Supun Wijerathne Jun 30 '16 at 05:21
  • @SlipperySeal some what agree with your 1st point. But there are DI frameworks that are used vastly in today like Guice which can be used to inject dependencies which makes some sense. And if you read this http://stackoverflow.com/questions/12150240/java-8-pre-release-interface-member-variables . You might think that my point makes some sense. And your 2nd point already has an answer in using same named default method in different interfaces for a class. – Supun Wijerathne Jun 30 '16 at 05:29
  • *Can someone explain why it doesn't make any sense* Why don't you try explaining why it does make sense? – shmosel Jun 30 '16 at 05:33
  • @shmosel good point. :) I wonder why abstract classes can have? And you may read my last comment. :) – Supun Wijerathne Jun 30 '16 at 05:35
  • 3
    Abstract classes can have a state, interfaces not - default methods only define an algorithm, but can not store any state other than as a local variable - and constructors are the mandatory entry point to ensure a state for an object which is useless for interfaces ("no state"). – Smutje Jun 30 '16 at 05:38
  • 2
    Try to explain which kind of problem you want to solve with an interface constructor. Then we can show you that the constructor won’t solve your problem. – Holger Jun 30 '16 at 08:29

2 Answers2

4

The main purpose of a constructor is to provide an instance of a defined type, which doesn't make sense on an interface, since the main purpose of an interface is provide contracts between the components in the code.

As for default constructors, it really doesn't make sense, since a default method has logic, what kind of logic would you declare on a default constructor?

J. Pichardo
  • 3,077
  • 21
  • 37
  • 2
    also, default methods solve a specific problem. default constructors would probably only cause problems, if they could even be implemented and work with the rules of the java type system – slipperyseal Jun 30 '16 at 05:26
  • @SlipperySeal, what is the specific problem that default methods solves? – Supun Wijerathne Jun 30 '16 at 05:33
  • 3
    @SupunWijerathne from the documentation: "Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces." – slipperyseal Jun 30 '16 at 05:37
  • @SlipperySeal thnx. :) But actually I didn't get those words. Anyway I'll search for a more simple explanation. – Supun Wijerathne Jun 30 '16 at 05:39
  • Default constructor could call a method of the interface, essentially forcing an initialization method call when a class implementing it is constructed. Wether it would be useful for anything, I don't know. – hyde Jun 30 '16 at 05:43
0

Constructors of what? Only classes have constructors, which create instances.

If an interface has methods that all have default implementations, you can create an instance like this:

MyFace obj = new MyFace(){};

It seems you're lamenting having to code two extra characters {}.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 1
    I'm not sure if this is really creating an "instance" of the interface. Your really creating an inline implementation. – Michael Markidis Jun 30 '16 at 05:49
  • Having interface default constructor would avoid hypothetical code like MyFace obj = new MyFace(){}; obj.init();´, so a bit more than just ´{}´, especially if calling init would be mandatory for implementation. – hyde Jun 30 '16 at 11:40
  • @hyde: that’s a nonsensical scenario, as there is nothing useful which `init()` can do. The object created via `new MyFace(){}` doesn’t contain any state that an `init()` method could modify. – Holger Jun 30 '16 at 15:01