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?

8
  • 3
    Why would you want to be able to instantiate an interface? Commented Jun 30, 2016 at 5: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? Commented Jun 30, 2016 at 5:20
  • Even not for that interface, it can be inherited by concrete child objects, same as an abstract class's constructor. Commented Jun 30, 2016 at 5:21
  • 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"). Commented Jun 30, 2016 at 5: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. Commented Jun 30, 2016 at 8:29

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

5 Comments

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, what is the specific problem that default methods solves?
@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 thnx. :) But actually I didn't get those words. Anyway I'll search for a more simple explanation.
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.
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 {}.

3 Comments

I'm not sure if this is really creating an "instance" of the interface. Your really creating an inline implementation.
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: 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.