Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance in Go

Tags:

inheritance

go

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)

like image 758
Murali Manohar Avatar asked Dec 08 '15 09:12

Murali Manohar


People also ask

Is there inheritance in Go?

Go does not support inheritance, however, it does support composition.

How does inheritance work in Go?

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.

Does Go support multiple inheritance?

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.

Why does Go not have OOP?

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.


2 Answers

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.

like image 177
Ainar-G Avatar answered Oct 14 '22 05:10

Ainar-G


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
like image 45
rox Avatar answered Oct 14 '22 05:10

rox