Why doesn't Go have Type Inheritance.
(The concept that when a class of an objects is defined, any subclass that is defined can inherit the definitions of one or more general classes)
Go does not support inheritance, however, it does support composition.
Inheritance means inheriting the properties of the superclass into the base class and is one of the most important concepts in Object-Oriented Programming. Since Golang does not support classes, so inheritance takes place through struct embedding.
There is no multiple inheritance in Go, even no single inheritance. Inheritance means that an inherited method is "overwritten", rather than being delegated to. This is not in the Go language as with OO languages (extends statement in Java, et.al.), but it can be done.
Is Go an object-oriented language? Yes and no. Although Go has types and methods and allows an object-oriented style of programming, there is no type hierarchy. The concept of “interface” in Go provides a different approach that we believe is easy to use and in some ways more general.
This was answered by the language creators in the F.A.Q.:
Object-oriented programming, at least in the best-known languages, involves too much discussion of the relationships between types, relationships that often could be derived automatically. Go takes a different approach.
Rather than requiring the programmer to declare ahead of time that two types are related, in Go a type automatically satisfies any interface that specifies a subset of its methods. Besides reducing the bookkeeping, this approach has real advantages. Types can satisfy many interfaces at once, without the complexities of traditional multiple inheritance. Interfaces can be very lightweight—an interface with one or even zero methods can express a useful concept. Interfaces can be added after the fact if a new idea comes along or for testing—without annotating the original types. Because there are no explicit relationships between types and interfaces, there is no type hierarchy to manage or discuss.
See also: Composition over inheritance principle.
If you need inheritance for reusable, this example shows how I reuse the width/height of a shape interface,
package main
// compare to a c++ example: http://www.tutorialspoint.com/cplusplus/cpp_interfaces.htm
import (
"fmt"
)
// interface
type Shape interface {
Area() float64
GetWidth() float64
GetHeight() float64
SetWidth(float64)
SetHeight(float64)
}
// reusable part, only implement SetWidth and SetHeight method of the interface
// {
type WidthHeight struct {
width float64
height float64
}
func (this *WidthHeight) SetWidth(w float64) {
this.width = w
}
func (this *WidthHeight) SetHeight(h float64) {
this.height = h
}
func (this *WidthHeight) GetWidth() float64 {
return this.width
}
func (this *WidthHeight) GetHeight() float64 {
fmt.Println("in WidthHeight.GetHeight")
return this.height
}
// }
type Rectangle struct {
WidthHeight
}
func (this *Rectangle) Area() float64 {
return this.GetWidth() * this.GetHeight() / 2
}
// override
func (this *Rectangle) GetHeight() float64 {
fmt.Println("in Rectangle.GetHeight")
// in case you still needs the WidthHeight's GetHeight method
return this.WidthHeight.GetHeight()
}
func main() {
var r Rectangle
var i Shape = &r
i.SetWidth(4)
i.SetHeight(6)
fmt.Println(i)
fmt.Println("width: ",i.GetWidth())
fmt.Println("height: ",i.GetHeight())
fmt.Println("area: ",i.Area())
}
Result:
&{{4 6}}
width: 4
in Rectangle.GetHeight
in WidthHeight.GetHeight
height: 6
in Rectangle.GetHeight
in WidthHeight.GetHeight
area: 12
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With