1

I have always had this question in my mind. An interface forces the implementer class to have specific methods, or properties, or events. But why couldn't it force the implementer class to have a specific constructor?

For example:

interface IX
{
    ctor(int val);

    string Foo();
}

..and this means that every class which implements IX should have a constructor with an int as a parameter.

Usage of this is not straight-forward, this only guarantees that if a class has implemented the IX interface, nobody in your development team has forgotten to put that constructor in his class. Consider the situation that these classes are instantiated by reflection or Activator.

Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
Reza
  • 18,865
  • 13
  • 88
  • 163
  • 1
    possible duplicate of [Interface defining a constructor signature?](http://stackoverflow.com/questions/619856/interface-defining-a-constructor-signature) – Jonesopolis Feb 03 '15 at 19:39
  • How would use ever consume a constructor on an interface? – SLaks Feb 03 '15 at 19:40

6 Answers6

3

The point of an interface is to define a set of members that can be used from the interface.

If you declare a function or property in an interface, you can call that function or property on any variable declared as that interface.

In contrast, there would never be any way to use a constructor declared on an interface.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • usage is not straight forward, this only guarantees if the class had implemented the interface you will be sure that the class has a specific constructor, I mean no body in the development team couldn't forget to put that constructor in his class – Reza Feb 03 '15 at 19:52
1

An interface doesn't have a constructor because an interface is not an instance of anything and there's nothing to construct. A certain type that implements an interface gets constructed when you create an instance of it so it can initialize its internal state.

An interface doesn't represent or encompass any kind of internal state (not even when some members of the interface expose data from the internal state of the type implementing it) so there's no need (or even meaning to) running a constructor for an interface.

Edit: your specific example ctor(int val); makes an assumption that all types implementing IX have some kind of int member variable but this is an implementation detail. The purpose of an interface is define what a type can do, not how it does it. The implementation of IX over different types may be very different, as long as they all provide a way internally to satisfy the interface.

For example, consider a type that needs a delimited string as input to work correctly:

class A : IX
{
    public A ( string sData ) { ... }

    // ...
}

The int required to satisfy IX could be part of the delimited string, yet, a single int constructor would be inappropriate for this class: a single int wouldn't be able to convey all other data needed for the correct operation of this class.

xxbbcc
  • 16,930
  • 5
  • 50
  • 83
  • 1
    How can you guarantee in your company all the developers has put a specific constructor in a class which is instantiated in run time by reflection or Activator? – Reza Feb 03 '15 at 20:01
  • Why would you need such a guarantee? You only need an explicit construcor if the default constructor is not enough - that is, a given type has to perform a specific set of operations before it can be used. There's no need for a constructor if a type can live with defaults generated by the compiler. – xxbbcc Feb 03 '15 at 20:02
  • Furthermore, a specific consturctor across several types doesn't make much sense, unless they share the same base class. If you have several independent types, it's not verly likely that they have matching internal states (even if they implement the same interface). If you have types sharing the same base class, the base class can have a constructor that provides the right signature and then derived classes can call that constructor as needed. – xxbbcc Feb 03 '15 at 20:04
  • Suppose that you traverse all the assemblies at startup of your application and find the classes which implement IX then you want to instantiate an object of them but with a specific constructor for example the one which takes a string as input, but one of the folks has forgotten to add this constructor, since the classes are not instantiated by `new` there will be no compile error, but in runtime you will get an exception – Reza Feb 03 '15 at 20:06
  • @RezaRahmati You're talking about a very specific situation. It could be useful there but the problems this presents outweighs the gain in this single, very specific case. As I said, have these classes share a base class and the base can define an appropriate constructor. Having a constructor like you describe in an interface assumes a very specific class layout which defeats the purpose of interfaces. – xxbbcc Feb 03 '15 at 20:09
  • Yes finally to solve my problem I made a base abstract class with that specific constructor, but I just wanted to know if having constructor in interface has any conflict with OO principals – Reza Feb 03 '15 at 20:11
0

Because of polymorphism I think it's not favorable to force the constructor in an interface. That's what first comes to mind.

grmbl
  • 2,514
  • 4
  • 29
  • 54
0

Because construction is highly dependent on implementation. There is no reason to specify a way to construct that would uniformize the construction. If you want to instanciate, you need to instanciate a class and then choose among the specific constructors provided. As you cannot instanciate an interface, what providing a construtor signature would mean?

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
0

It could. However, an interface is simply a contract to fulfill - it doesn't matter how an implementation fulfills that contract. Forcing the implementer to have a specific constructor needlessly constrains them in their implementation, and serves no benefit - if you wanted to enforce certain parameters for construction of an object, it would be easier and more flexible to have them implement a factory method.

Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
0

When you think of an interface as of a way to encapsulate a behaviour rather than to impose a specific implementation then it totally makes sense not to have a constructor in it. The constructor has no influence on the encapsulated behaviour but rather it shapes the structure of a class and the way the class is initialized.

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68