## Implementing interfaces

Once the interface is defined, implementing it is rather trivial.

The first step is to define a normal final GObject class exactly as in the section called “Boilerplate header code”.

The second step is to implement MamanBaz by defining it using `G_DEFINE_TYPE_WITH_CODE` and `G_IMPLEMENT_INTERFACE` instead of `G_DEFINE_TYPE`:

    1
    2
    3
    4
    5

    static void maman_ibaz_interface_init (MamanIbazInterface *iface);

    G_DEFINE_TYPE_WITH_CODE (MamanBar, maman_bar, G_TYPE_OBJECT,
                             G_IMPLEMENT_INTERFACE (MAMAN_TYPE_IBAZ,
                                                    maman_ibaz_interface_init))

This definition is very much like all the similar functions seen previously. The only interface-specific code present here is the use of `G_IMPLEMENT_INTERFACE`.

Classes can implement multiple interfaces by using multiple calls to `G_IMPLEMENT_INTERFACE` inside the call to `G_DEFINE_TYPE_WITH_CODE`

`maman_baz_interface_init`, the interface initialization function: inside it every virtual method of the interface must be assigned to its implementation:

    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

    static void
    maman_baz_do_action (MamanBaz *self)
    {
      g_print ("Baz implementation of Ibaz interface Action: 0x%x.\n",
               self->instance_member);
    }

    static void
    maman_baz_do_something (MamanBaz *self)
    {
      g_print ("Baz implementation of Ibaz interface Something: 0x%x.\n",
               self->instance_member);
    }

    static void
    maman_ibaz_interface_init (MamanIbazInterface *iface)
    {
      iface->do_action = maman_baz_do_action;
      iface->do_something = maman_baz_do_something;
    }

    static void
    maman_baz_init (MamanBaz *self)
    {
      self->instance_member = 0xdeadbeef;
    }