## Overriding interface methods

If a base class already implements an interface and a derived class needs to implement the same interface but needs to override certain methods, you must reimplement the interface and set only the interface methods which need overriding.

In this example, ViewerAudioFile is derived from ViewerFile. Both implement the ViewerEditable interface. ViewerAudioFile only implements one method of the ViewerEditable interface and uses the base class implementation of the other.

    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

    static void
    viewer_audio_file_editable_save (ViewerEditable  *editable,
                                     GError         **error)
    {
      ViewerAudioFile *self = VIEWER_AUDIO_FILE (editable);

      g_print ("Audio file implementation of editable interface save method.\n");
    }

    static void
    viewer_audio_file_editable_interface_init (ViewerEditableInterface *iface)
    {
      /* Override the implementation of save(). */
      iface->save = viewer_audio_file_editable_save;

      /*
       * Leave iface->undo and ->redo alone, they are already set to the
       * base class implementation.
       */
    }

    G_DEFINE_TYPE_WITH_CODE (ViewerAudioFile, viewer_audio_file, VIEWER_TYPE_FILE,
                             G_IMPLEMENT_INTERFACE (VIEWER_TYPE_EDITABLE,
                                                    viewer_audio_file_editable_interface_init))

    static void
    viewer_audio_file_class_init (ViewerAudioFileClass *klass)
    {
      /* Nothing here. */
    }

    static void
    viewer_audio_file_init (ViewerAudioFile *self)
    {
      /* Nothing here. */
    }

To access the base class interface implementation use `g_type_interface_peek_parent` from within an interface's `default_init` function.

To call the base class implementation of an interface method from an derived class where than interface method has been overridden, stash away the pointer returned from `g_type_interface_peek_parent` in a global variable.

In this example ViewerAudioFile overrides the `save` interface method. In its overridden method it calls the base class implementation of the same interface method.

    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

    static ViewerEditableInterface *viewer_editable_parent_interface = NULL;

    static void
    viewer_audio_file_editable_save (ViewerEditable  *editable,
                                     GError         **error)
    {
      ViewerAudioFile *self = VIEWER_AUDIO_FILE (editable);

      g_print ("Audio file implementation of editable interface save method.\n");

      /* Now call the base implementation */
      viewer_editable_parent_interface->save (editable, error);
    }

    static void
    viewer_audio_file_editable_interface_init (ViewerEditableInterface *iface)
    {
      viewer_editable_parent_interface = g_type_interface_peek_parent (iface);

      iface->save = viewer_audio_file_editable_save;
    }

    G_DEFINE_TYPE_WITH_CODE (ViewerAudioFile, viewer_audio_file, VIEWER_TYPE_FILE,
                             G_IMPLEMENT_INTERFACE (VIEWER_TYPE_EDITABLE,
                                                    viewer_audio_file_editable_interface_init))

    static void
    viewer_audio_file_class_init (ViewerAudioFileClass *klass)
    {
      /* Nothing here. */
    }

    static void
    viewer_audio_file_init (ViewerAudioFile *self)
    {
      /* Nothing here. */
    }