Inheritance in GoLang

22 Jun, 2020
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. We cannot directly extend structs but rather use a concept called composition where the struct is used to form other objects. So, you can say there is No Inheritance Concept in Golang.
In composition, base structs can be embedded into a child struct and the methods of the base struct can be directly called on the child struct as shown in the following example.
Example 1:
// Golang program to illustrate the// concept of inheritancepackage main  import (    "fmt")  // declaring a struct type Comic struct{      // declaring struct variable    Universe string}  // function to return the// universe of the comicfunc (comic Comic) ComicUniverse() string {      // returns comic universe    return comic.Universe}  // declaring a structtype Marvel struct{          // anonymous field,    // this is composition where     // the struct is embedded    Comic}  // declaring a structtype DC struct{          // anonymous field    Comic}  // main functionfunc main() {          // creating an instance    c1 := Marvel{              // child struct can directly        // access base struct variables            Comic{            Universe: "MCU",            },        }                        // child struct can directly    // access base struct methods          // printing base method using child        fmt.Println("Universe is:", c1.ComicUniverse())               c2 := DC{        Comic{            Universe : "DC",        },    }          // printing base method using child    fmt.Println("Universe is:", c2.ComicUniverse())}
Output:
Universe is: MCU
Universe is: DC

Multiple inheritances take place when the child struct is able to access multiple properties, fields, and methods of more than one base struct. Here the child struct embeds all the base structs as shown through the following code:
Example 2:
// Golang program to illustrate the// concept of multiple inheritancespackage main  import (    "fmt")  // declaring first // base struct type first struct{      // declaring struct variable    base_one string}  // declaring second// base structtype second struct{      // declaring struct variable    base_two string}  // function to return// first struct variablefunc (f first) printBase1() string{      // returns a string    // of first struct            return f.base_one}  // function to return// second struct variablefunc (s second) printBase2() string{      // returns a string    // of first struct    return s.base_two}  // child struct which// embeds both base structstype child struct{      // anonymous fields,    // struct embedding    // of multiple structs    first    second}  // main functionfunc main() {          // declaring an instance     // of child struct    c1 := child{              // child struct can directly        // access base struct variables        first{                base_one: "In base struct 1.",        },        second{            base_two: "\nIn base struct 2.\n",        },    }          // child struct can directly    // access base struct methods          // printing base method    // using instance of child struct    fmt.Println(c1.printBase1())    fmt.Println(c1.printBase2())}
Output:
In base struct 1.

In base struct 2.

Golang-OOPs
Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.