Open In App

Inheritance in GoLang

Last Updated : 22 Jun, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 inheritance
package main
  
import (
    "fmt"
)
  
// declaring a struct 
type Comic struct{
  
    // declaring struct variable
    Universe string
}
  
// function to return the
// universe of the comic
func (comic Comic) ComicUniverse() string {
  
    // returns comic universe
    return comic.Universe
}
  
// declaring a struct
type Marvel struct{
      
    // anonymous field,
    // this is composition where 
    // the struct is embedded
    Comic
}
  
// declaring a struct
type DC struct{
      
    // anonymous field
    Comic
}
  
// main function
func 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 inheritances
package main
  
import (
    "fmt"
)
  
// declaring first 
// base struct 
type first struct{
  
    // declaring struct variable
    base_one string
}
  
// declaring second
// base struct
type second struct{
  
    // declaring struct variable
    base_two string
}
  
// function to return
// first struct variable
func (f first) printBase1() string{
  
    // returns a string
    // of first struct        
    return f.base_one
}
  
// function to return
// second struct variable
func (s second) printBase2() string{
  
    // returns a string
    // of first struct
    return s.base_two
}
  
// child struct which
// embeds both base structs
type child struct{
  
    // anonymous fields,
    // struct embedding
    // of multiple structs
    first
    second
}
  
// main function
func 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.


Next Article

Similar Reads

Article Tags :
three90RightbarBannerImg
  翻译: