Welcome To Golang By Example

Inheritance in GO using interface

This post describes inheritance using interface. Do visit our Inheritance in Go Complete Guide post for full reference

Go supports inheritance by embedding struct or using interface. There are different ways of doing it and each having some limitations. The different ways are:

  1. By using embedded struct – The parent struct is embedded in child struct. The limitation is that subtyping is not possible with this approach. You cannot pass the child struct to a function that expects base. Refer this link for more details –Inheritance using struct
  2. By using interfaces – Subtyping is possible but the limitation is that one has no way to refer to common properties. Current post describes this approach
  3. By using interface + struct – This fixes the limitations of above two approach but one limitation is that overriding methods is not possible. But there is a workaround. Refer to this link for more details – Inheritance using interface + struct

Details:

The child struct implements the methods of a common interface. This approach also solves the problem of subtyping. See below code

package main

import "fmt"

type iBase interface {
	say()
}

type child struct {
	style string
}

func (b *child) say() {
	fmt.Println(b.style)
}

func check(b iBase) {
	b.say()
}

func main() {
	child := &child{
		style: "somestyle",
	}
	child.say()
	check(child)
}

Output:

somestyle
somestyle

Limitation:

This approach has a limitation that it is not possible to refer to common properties as an interface can not have any properties. This problem is solved by the mixed approach of using Struct + Interface.