Does Go have method override / overwrite?

i know Go can do things similar to method override through embeding and delegation.

according to wikipedia Method_overriding , method override seems to a term only in object oriented language.so since Go is not an object-oriented language and has no inheritance, i think Go has no method override, but similar mechanism provided by embeding and delegation.

how do you think?

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

@radovskyb
hi, thanks for your reply!
according to wikipedia inheritance, inheritance has similar definition to method override.

In object-oriented programming, inheritance is …

so can we say Golang has inheritance ?
but in Go for Java Programmers , Sameer Ajmani, the Tech Lead Manager of go team, said that Go intentionally leaves out inheritance.

and also, Golang’s delegation is different from OOP’s method override in some way.

package main

import "fmt"

func main() {
	var s son = son{}
	s.shoutLouder()
}

type father struct {
}

func (self father) shoutLouder() {
	fmt.Println("louder...") 
	self.shout()
}
func (f father) shout() {
	fmt.Println("father shouting")
}

type son struct {
	father
}

func (s son) shout() {
	fmt.Println("son shouting")
}

in this example, father’s shout() method will be called, but in a regular OOP language, son’s shout() will be called.

and finally, i can not find any official articles that mention Golang has method override.

1 Like

That’s exactly it. Benjamins example demonstrates that embedding does provide method overriding. There is no need for superclasses, subclasses, and inheritance mechanisms.

For completeness, let me just add that overriding methods in Go is not even necessary in many scenarios. Interfaces in Go are usually very small. Often, they only contain one or two methods. So if you need a method that accepts all three types (Object, ObjectOne, and ObjectTwo), simply define a method that accepts a Stringer interface:

func printObject(s fmt.Stringer) {
    fmt.Println(s)
}

Then you can pass any type that implements the Stringer interface:

printObject(o)
printObject(o1)
printObject(o2)

For this, ObjectOne and ObjectTwo do not even need to embed Object and override its String method. Just define a String() method for each of them and you’re done.

Bottom line: Be careful when trying to map idioms, patterns, or techniques from other programming languages to Go. Often, this does work somehow but there is usually a simpler way in Go.

2 Likes

A great blog post on this topic was posted just a few days ago on goinggo.net.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.