Does Go have method override / overwrite?

Hey @BruceAuyeung,

The first 6 words from the article you posted reads:
Method overriding, in object oriented programming. The key word here being in.
In my opinion, It does not mean that this is limited to only purely object oriented programming.

Also from the article:
The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name

When using composition to allow one object to inherit (so to speak) methods from another object, why would you then say that this is not technically method overriding when one of the methods that’s been inherited by composition gets overridden by the object inheriting it, by declaring another method with the same name?

package main

import "fmt"

// Parent Object.
type Object struct{}

func (b Object) String() string {
	return "I am an Object"
}

type ObjectOne struct {
	Object
}

type ObjectTwo struct {
	Object
}

func (b ObjectTwo) String() string {
	return "I am an Object Two"
}

func main() {
	o := Object{}
	fmt.Println(o) // I am an Object

	o1 := ObjectOne{}
	fmt.Println(o1) // I am an Object

	o2 := ObjectTwo{}
	fmt.Println(o2) // I am an Object Two
}
1 Like