Golang Program to Show Inheritance in Class


In this article, we are going to learn to show inheritance in class using go programming.

Inheritance In Golang − One of the key ideas in object-oriented programming is inheritance, which refers to passing on the properties of the superclass to the base class. As classes are not supported by Golang, inheritance occurs through struct embedding. Since structs cannot be directly extended, we must instead use the idea of composition to create new objects using the struct. So, it's safe to say that Golang doesn't support inheritance.

Example 1

Golang Program to Show Inheritance in Class

The following code shows how can we show inheritance in the go programming language.

package main

import (
   "fmt"
)
// fmt package allows us to print anything

// defining a struct named games and defining a string variable in it
type games struct {
   new string
}

// function to return the string variable of the games struct
// this function is defined on the games struct and returns the string value
func (games games) AllGames() string {

   // returns new variable
   return games.new
}

// declaring another struct named cricket
type Cricket struct {

   // embedding the games struct in the cricket struct
   games
}

// declaring a struct named hockey
type hockey struct {
   // embedding the games struct in the hockey struct
   games
}

// calling the main function
func main() {

   // creating an instance to the cricket struct
   I1 := Cricket{

      // child struct can directly
      // access base struct variables
      games{
         new: "ICC tournaments",
      },
   }
   // storing the result in a variable called result
   result := I1.AllGames()

   // Accessing child struct directly from struct methods
   // printing base method using child
   fmt.Println("New game is:", result)

   // creating another instance to the hockey struct
   I2 := hockey{
      games{
         new: "Hockey tournaments",
      },
   }

   // Accessing child struct directly from struct methods
   // printing base method using child
   fmt.Println("New game is:", I2.AllGames())
}

Output

New game is: ICC tournaments
New game is: Hockey tournaments

Description

  • First, we need to import the fmt package that allows us to print anything on the screen.

  • Then we need to define a struct. This will be the parent struct that will be equivalent to the parent class. Also, we have defined a function for this struct that will return the name of the tournament as a string.

  • Further, we are creating two new structs named cricket and hockey these structs inherit all the properties of the parent struct named games and will be child structs or classes.

  • Call the main function this is the starting point of our program and the code executes from this point.

  • Create an instance of the cricket struct and define the new string present in it. Note that the string named new was not initially defined in this struct but on the games struct the only reason we are able to use the properties of that struct is that we have inherited its properties in the cricket struct.

  • Now call the AllGames() function defined to the games struct this.

  • Store the value it returns in a variable called result and print it on the screen using fmt.Println().

  • Repeat the above steps by creating the instance to the hockey struct and printing the result to the screen.

Example 2

Golang Program to Show Multiple Inheritences to a Class

package main

import (
   "fmt"
)

// fmt package allows us to print anything on the screen

// declaring first struct and defining a string type property in it
type first struct {

   // declaring struct variable
   base_one string
}

// declaring second struct and defining a string type property in it
type second struct {
   // declaring struct variable
   base_two string
}

// defining the function to the first struct naming PrintBase()
// this function returns a string
func (f first) printBase1() string {

   // returning the result
   return f.base_one
}

// defining the function to the second struct naming PrintBase()
// this function returns a string
func (s second) printBase2() string {

   // returning the result
   return s.base_two
}

// defining the child struct
// inheriting the properties of first and second struct to it
type child struct {

   // inheriting the parent structs first and second
   first
   second
}

// calling the main function
func main() {

   // creating an instance to the child struct
   c1 := child{

      // assigning values to parent struct through instance of child struct
      first{
         base_one: "\nmango",
      },
      second{
         base_two: "apple\n",
      },
   }

   // child struct can directly
   // access base struct methods

   // calling the function defined on the first and second struct using the child structs
   fmt.Println("Printing the values by calling the method defined on first and second struct using instance defined on child struct")
   fmt.Println(c1.printBase1())
   fmt.Println(c1.printBase2())
}

Output

Printing the values by calling the method defined on first and second struct using instance defined on child struct

mango
apple

Description

  • First, we need to import the fmt package that allows us to print anything on the screen.

  • Then we have defined two structs named first and second. These will be the parent structs. Also, we have defined a function for these structs that will return a string.

  • Then, we need to create a child struct this struct will inherit all the properties of the parent struct.

  • Call the main function.

  • Create an instance of the child struct and define the properties of the parent structs present in it. Note that the string was not initially defined in this struct but on the first and second struct the only reason we are able to use the properties of that struct is that we have inherited its properties in the child struct.

  • Now call the printBase() function.

  • Print it on the screen using fmt.Println().

Updated on: 29-Dec-2022

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements