1

I know the the concept of interface and implementation of it also

I know properties and method definitions can be written interface .

But while going through topics related i came to know that we cant declare a variable in interface . Just wanted to know the reason for it ?

Eric J.
  • 147,927
  • 63
  • 340
  • 553
Kuntady Nithesh
  • 11,371
  • 20
  • 63
  • 86
  • Have a look at [this](http://stackoverflow.com/questions/2115114/why-cant-c-interfaces-contain-fields) previous post. – Vladimir Aug 12 '11 at 06:14

8 Answers8

4

Because variables and fields are the implementation. Interfaces are contracts that declare what they do, not how they do it.

If you want to declare fields, then you need to create a base (possibly abstract) class.

From consumer point of view, an important difference between an interface an an abstract base class is that you can derive your class from only one base class while your class may implement as many interfaces as you want.

Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
1

Interfaces are intended to describe behavior, not implementation.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
1

Because a variable defines how things are stored inside your object. This is not compatible with the idea of an interface.

Besides it would lead to multiple-inheritance-style problems like

interface A { int x; }
interface B { int x; }
class C : A, B

Which instance of x should be included in C (one och both and how do you differ them in that case?)

You should use a property in your interface as that can be defined with out specifying the implementation. That serves the same purpose but with better encapsulation.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
  • Assuming that we could have an interface declaring a field, and that it would simply mean that any class implementing that interface must provide a public field of the same type with the same name, I don't see how the question "which instance of x should be included in C?" is relevant. The field `x` on both interfaces would simply refer to the same thing in memory. Think about a class `C` implementing two interfaces `A` and `B` which both declare a `void x();`. Do you see any problem here? – Bruno Reis Aug 12 '11 at 06:40
  • I think the *multiple-inheritance-style problems* mentioned by @Albin refer to the *diamond problem*, that is, when you have a class `D` that inherits from both classes `B` and `C`, that override some method in the common parent class `A`. If you call that method in an instance of `D`, which code would be executed? – Bruno Reis Aug 12 '11 at 06:46
1

Lets say that it can be defined. So:

interface Foo
{
     int Number;
     string Text;
}

class Bar : Foo
{
     public int Number;
     public string Text;
}

So, in each derived class (class that implements Foo interface) you would have to create two public members. That, at least to me, makes no sense.

If you want your classes to have some members that are not methods, and you would like to simplify it as much as possible, take a look at Auto-Implemented Properties.

Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99
0

Beyond "because that's the spec" I'm not sure, one reason might be that it seems to me that everything allowed in an interface can be tied directly to a method ( Properties map one or to two methods ).

IanNorton
  • 7,145
  • 2
  • 25
  • 28
0

Whats the use of an interface? Its basically acts as contract. So whats the point of having a predefined format of a variable declared in ur contract? No use. Plus its like a template.

Zenwalker
  • 1,883
  • 1
  • 14
  • 27
0

The idea of an interface is to declare the parts of a type accissible to the outside world or reversely not to care about implementation details. Since fields should be regarded as implementation details it would be a contradiction even to be able to declare them

Rune FS
  • 21,497
  • 7
  • 62
  • 96
0

Interface acts as a contract. So, it can't contain the variable declaration. However, it may contain Properties declation, that you will implement in the inherited class.

FIre Panda
  • 6,537
  • 2
  • 25
  • 38