Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are we not allowed to specify a constructor in an interface? [duplicate]

Tags:

c#

.net

oop

vb.net

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.

like image 459
Pondidum Avatar asked Mar 27 '09 11:03

Pondidum


People also ask

Why we Cannot use constructor in interface?

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.

Can you define a constructor in an interface?

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?

What happens when a constructor is defined for an interface? Explanation: Constructor is not provided by interface as objects cannot be instantiated.

Can we define constructor in interface C#?

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


1 Answers

Because an interface describes behaviour. Constructors aren't behaviour. How an object is built is an implementation detail.

like image 101
cletus Avatar answered Sep 24 '22 04:09

cletus