Interfaces and properties

11,589

Solution 1

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.

Solution 2

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.

Share:
11,589
Vegar
Author by

Vegar

SOreadytohelp

Updated on June 15, 2022

Comments

  • Vegar
    Vegar almost 2 years

    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?