Golang program to show overloading of methods in class


In this article, we will learn how to overload the method in class using different example. Go programming language does not have concept of a class so function overloading will be used to execute the program. As a result, code can be more flexible and readable. As an illustration, you could create a function that accepts an interface as a parameter and then call it with several types that implement the interface. Let’s see it execution.

Method 1: using custom type MyInt

Here, it contains two methods Print() and PrintWithNumber() where values can be sent in these functions of MyInt type.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program

  • Step 2 − Create a unique kind an alias for the built-in int type is MyInt.

  • Step 3 − Define the MyInt type's two associated methods, Print() and PrintWithNumber(number string).

  • Step 4 − In the main() function create a variable of type MyInt named val and give it value 6.

  • Step 5 − To print the value 6, use the Print() function on the val variable.

  • Step 6 − Call the PrintWithNumber(number string) function with the argument "number" on the val variable. This will print the number 6 and the word "number" after it.

  • Step 7 − The print statement is executed using fmt.Println() function where ln means new line.

Example

In this example we will see how to overload methods in class using custom type MyInt.

package main
import "fmt"

type MyInt int

func (val MyInt) Print() {
	fmt.Println(val)   //print val created in main
}

func (val MyInt) PrintWithNumber(number string) {
	fmt.Println(val, number)    //print both val of type MyInt and number of type string
}

func main() {
	val := MyInt(6)   //create val
	fmt.Println("The representation of function overloading is:")
	val.Print()                   // Output: 6
	val.PrintWithNumber("number") // Output: 6 number
}

Output

The representation of function overloading is:
6
6 number

Method: Using interface with MyInt type

Here, the .. implies that any number of arguments can be taken by the print function. Let’s see the code and algorithm to know how it’s done.

Syntax

func len(v Type) int

The len() function is used to get the length of any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program

  • Step 2 − Create an alias for the built-in int type MyInt.

  • Step 3 − Create a method print which is associated with the MyInt type, such that print(args...interface). Here, … helpd to take any number of arguments.

  • Step 4 − In the next step check the length of the args slice using an if statement inside the print method.

  • Step 5 − Print the value of the MyInt type if the length of the args slice is zero and print the value of the MyInt type, followed by the args slice, if the length of the args slice is larger than zero.

  • Step 6 − Create a variable of type MyInt named val and give it the value 6 in the main() method.

  • Step 7 − Print the value of val using fmt.Println() function where ln means new line.

Example

In this example we will learn how to show overloading of methods in class using interface with MyInt type.

package main
import "fmt"
type MyInt int

func (val MyInt) Print(args ...interface{}) {
	if len(args) == 0 {         //if the length of args slice is 0 print if statement 
		fmt.Println(val)
	} else {
		fmt.Println(val, args) //else print it 
	}
}

func main() {
	val := MyInt(6)  //create val
	fmt.Println("The function overloading is represented here:")
	val.Print()         // Output: 6
	val.Print("number") // Output: 6 [number]
}

Output

The function overloading is represented here:
6
6 [number]

Conclusion

We executed the program of overloading the methods using two examples. In the first example we used custom type MyInt whereas in the second example we used interface with type MyInt. Both the programs gave similar output.

Updated on: 01-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements