Search code examples
inheritanceinterfacegocomposition

Composition and multiple inheritance


Since Go uses an composition system instead of (multiple) inheritance, I'm just wondering about these 3 code snippets. Go says they force the programmer to use composition.

A) should be (almost) correct Go-Code, B) pseudo C) pseudo

Imho the result will always be the same on all three codes, beside the fact, that B) and C) can be used for even more stuff and A) forces you to stick to composition?

Even if you assume B) to not have the sort-method inside of the class but - lets say global like A) doesn't make a real difference oO

A) Go code:

interface Sort
    Len()
    Less(i, j int) bool
    Swap(i, j int)

func (qs *Sort) sort()
    doTheSorting

type MyData struct {
    var value int
}

func (s *MyData) Len() { ... }
func (s *MyData) Less(i, j int) bool { ... }
func (s *MyData) Swap(i, j int) { ... }

B) Looks like Inheritance but can imho be seen as embedded, according to how the compiler works.

class Sort

    public sort() { ... }

    abstract Len()
    abstract Less(i, j int) bool
    abstract Swap(i, j int)

C)

interface SortInterface
    void Len()
    bool Less(i, j int)
    void Swap(i, j int)

class Sort implements SortInterface
    public sort() { ... }

Usage B and C:

class MyClass **embed** Sort

    int value

    void Len() { ... }
    bool Less(i, j int) { ... }
    void Swap(i, j int) { ... }

Solution

  • No this is not how go works. Here is an example (pulled from the standard library) of a type that can be sorted.

    type IntSlice []int
    
    func (p IntSlice) Len() int           { return len(p) }
    func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
    func (p IntSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
    

    This implements the interface:

    // Name of interface changed for clarity
    type Sort interface {
        Len() int
        Less(i, j int) bool
        Swap(i, j int)
    }
    

    A type which implement the Sort interface does not get a new method. You can not assign a method to an interface such as in your example func (qs *Sort) sort() {...}.

    However, it is allowed to be passed to functions and methods expecting a variable of type Sort. Because of this, I am able to call sort.Sort(myIntSlice) and it will then be sorted.

    Here is an example function which takes any parameter that implements the Sort interface:

    func IsSorted(data Sort) bool {
        n := data.Len()
        for i := n - 1; i > 0; i-- {
            if data.Less(i, i-1) {
                return false
            }
        }
        return true
    }
    

    In IsSorted, the function has no idea what the true type of data is. It could be IntSlice or anything else. What it does know is that whatever parameter you gave it implements the methods in the Sort interface.

    I do not seem to be able to figure out the question you asked however. Also, psuedo code is very difficult to understand. Using another language such as java would have been better.