Possible Duplicate:
Interface defining a constructor signature?
I know that you cannot specify a constructor in an interface in .Net, but why can we not?
It would be really useful for my current project to be able to specify that an 'engine' must be passed in with the constructor, but as I cant, I have to suffice with an XML comment on the class.
Constructor in an interface An Interface in Java doesn't have a constructor because all data members in interfaces are public static final by default, they are constants (assign the values at the time of declaration). There are no data members in an interface to initialize them through the constructor.
No, you cannot have a constructor within an interface in Java. You can have only public, static, final variables and, public, abstract, methods as of Java7. From Java8 onwards interfaces allow default methods and static methods.
What happens when a constructor is defined for an interface? Explanation: Constructor is not provided by interface as objects cannot be instantiated.
The technical answer is that you can't; defining a constructor on an Interface is not allowed in any programming language that I know of, definitely not in C#.
Because an interface describes behaviour. Constructors aren't behaviour. How an object is built is an implementation detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With