views:

539

answers:

2

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?

+18  A: 

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
+3  A: 

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