Reza
Reza

Reputation: 19843

Why can't interface have constructor?

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.

Upvotes: 2

Views: 4077

Answers (6)

xxbbcc
xxbbcc

Reputation: 17327

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.

Upvotes: 1

PiotrWolkowski
PiotrWolkowski

Reputation: 8782

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.

Upvotes: 0

Chris Mantle
Chris Mantle

Reputation: 6683

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.

Upvotes: 0

Jean-Baptiste Yunès
Jean-Baptiste Yunès

Reputation: 36401

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?

Upvotes: 0

SLaks
SLaks

Reputation: 887385

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.

Upvotes: 3

ʃʈɑɲ
ʃʈɑɲ

Reputation: 2684

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

Upvotes: 0

Related Questions