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.
It is inheritance but probably not the sort of inheritance you are probably after. Your example look promising b/c Person
and Cat
are behaviorally and structurally equal to each other modulo the type names.
As soon as you'd attempt to use this "inheritance" to 'extend' some base type with, for example added fields, you'll find that the receiver of the "base class" is always the base class, never the extended one. IOW, you cannot achieve structurally polymorphous type hierarchy.
OTOH, Go supports purely behavioral inheritance via interfaces. Embedding one interface into another does create an inheritance tree.
package main
import "fmt"
type Thing struct {
Name string
Age int
}
func (t *Thing) me() {
fmt.Printf("I am a %T.
", t)
}
type Person struct {
Thing
}
func (p *Person) Iam() {
fmt.Printf("I am a %T.
", p)
}
type Cat struct {
Thing
}
func (c *Cat) Iam() {
fmt.Printf("I am a %T.
", c)
}
func main() {
var p Person
var c Cat
p.me()
p.Iam()
c.me()
c.Iam()
}
It's called composition. Methods of Person or Cat have no direct access to the fields of Thing. Also if e.g. Cat implements an own SetAge() and you want to call the one of Thing you would have to call myCat.Thing.SetAge(42) instead of myCat.SetAge(42).
Since you mentioned C#, try to do this in go. Method calls can't be made virtual in Go (except through interfaces).
// this is C#, not go
public class Thing
{
public virtual string Name {get; set;}
public virtual int Age {get; set;}
}
public class Person : Thing {}
public class Cat : Thing
{
public override int Age
{
get
{
return base.Age * 7; //age in cat's years
}
}
}
and call it like this:
Thing t = new Cat() {Name="Tom", Age=5}; // a Cat object is assigned
// to a Thing variable
Console.WriteLine(t.Age); // outputs 35.
// t.Age refers to Cat's implementation
// of Age, not Thing's. Notice how Age is virtual
// in Thing and overridden in Cat