33

Is it possible to declare a property in an interface without declaring the get- and set-methods for it? Something like:

IValue = interface
  property value: double;
end;

I want to state that the implementor should have a property called value, returning a double, but I really don't care if it returns a private field or the result from a function.

If it is possible, is it possible to declare it read/write or read-only?

Vegar
  • 12,828
  • 16
  • 85
  • 151

2 Answers2

41

No. Interfaces are implemented as function tables (basically a simple virtual method table) and the compiler needs to know there's a function to map the property onto. You can declare a property on an interface, but it has to have functions as getter/setter values, not fields. You can make it read-only or write-only, though.

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
8

When working with properties in an interface, think of the property as a shortcut to the reader/writer. Only one is required to satisfy the shortcut...otherwise it doesn't point to anything.

skamradt
  • 15,366
  • 2
  • 36
  • 53
  • 6
    Yep. In fact, the property declaration is purely there for your convenience. If you create an interface with a property on it, and put it on a class that implements the functions but doesn't declare the property, that class will compile just fine. – Mason Wheeler Aug 11 '09 at 16:51