## Interface properties

GObject interfaces can also have properties. Declaration of the interface properties is similar to declaring the properties of ordinary GObject types as explained in the section called “Object properties”, except that `g_object_interface_install_property` is used to declare the properties instead of `g_object_class_install_property`.

To include a property named 'name' of type string in the MamanIbaz interface example code above, we only need to add one call in `maman_ibaz_default_init` as shown below:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    static void
    maman_ibaz_default_init (MamanIbazInterface *iface)
    {
      g_object_interface_install_property (iface,
                                           g_param_spec_string ("name",
                                                                "Name",
                                                                "Name of the MamanIbaz",
                                                                "maman",
                                                                G_PARAM_READWRITE));
    }

One point worth noting is that the declared property wasn't assigned an integer ID. The reason being that integer IDs of properties are used only inside the `get_property` and `set_property` virtual methods. Since interfaces declare but do not implement properties, there is no need to assign integer IDs to them.

An implementation declares and defines its properties in the usual way as explained in the section called “Object properties”, except for one small change: it can declare the properties of the interface it implements using `g_object_class_override_property` instead of `g_object_class_install_property`. The following code snippet shows the modifications needed in the MamanBaz declaration and implementation above:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65

    struct _MamanBaz
    {
      GObject parent_instance;

      gint instance_member;
      gchar *name;
    };

    enum
    {
      PROP_NAME = 1,
      N_PROPERTIES
    };

    static void
    maman_baz_set_property (GObject      *object,
                            guint         prop_id,
                            const GValue *value,
                            GParamSpec   *pspec)
    {
      MamanBaz *baz = MAMAN_BAZ (object);

      switch (prop_id)
        {
        case PROP_NAME:
          g_free (baz->name);
          baz->name = g_value_dup_string (value);
          break;

        default:
          G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
          break;
        }
    }

    static void
    maman_baz_get_property (GObject    *object,
                            guint       prop_id,
                            GValue     *value,
                            GParamSpec *pspec)
    {
      MamanBaz *baz = MAMAN_BAZ (object);

      switch (prop_id)
        {
        case PROP_NAME:
          g_value_set_string (value, baz->name);
          break;

        default:
          G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
          break;
        }
    }

    static void
    maman_baz_class_init (MamanBazClass *klass)
    {
      GObjectClass *object_class = G_OBJECT_CLASS (klass);

      object_class->set_property = maman_baz_set_property;
      object_class->get_property = maman_baz_get_property;

      g_object_class_override_property (object_class, PROP_NAME, "name");
    }