I have heard a lot of people talk about Go, and how it does not support inheritance. Until actually using the language, I just went along with the crowd and listened to the hear say. After a little messing about with the language, getting to grips with the basics. I came across this scenario:
package main
type Thing struct {
Name string
Age int
}
type UglyPerson struct {
Person
WonkyTeeth bool
}
type Person struct {
Thing
}
type Cat struct {
Thing
}
func (this *Cat) SetAge(age int){
this.Thing.SetAge(age)
}
func (this *Cat GetAge(){
return this.Thing.GetAge() * 7
}
func (this *UglyPerson) GetWonkyTeeth() bool {
return this.WonkyTeeth
}
func (this *UglyPerson) SetWonkyTeeth(wonkyTeeth bool) {
this.WonkyTeeth = wonkyTeeth
}
func (this *Thing) GetAge() int {
return this.Age
}
func (this *Thing) GetName() string {
return this.Name
}
func (this *Thing) SetAge(age int) {
this.Age = age
}
func (this *Thing) SetName(name string) {
this.Name = name
}
now, what this does it composes the Person and Cat Structs, from the Thing Struct. By doing so, not only does the Person and Cat struct share the same Fields as the Thing Struct, but also, through composition, the methods of Thing are also shared. Is this not inheritance? Also by implenting an interface as such:
type thing interface {
GetName() string
SetName(name string)
SetAge(age int)
}
All three Structs are now joined or should I say, can be used in a homogenous fashion, such as an array of "thing".
So, I lay it on you, is this not inheritance?
Edit
Added a new derived Struct called "Ugly person" and Overridden the SetAge method for Cat.