What is polymorphism in Go?
Overview
Polymorphism is an ability of a thing to be displayed in multiple forms. For example, a human can be a child, an adult, a sibling, a parent, and so on. Thus, the same human exhibits different forms based on situations, and this is what we call polymorphism.
In a more technical term, polymorphism can be defined as a function that can be used for different types. However, that function must have different signatures.
Example
In Go, we can implement polymorphism using interfaces only.
Let's see an example in the code snippet below:
package main// importing package 'fmt'import "fmt"// creating an interface 'Language'type Language interface {getDevelopedBy() string}// creating a structure 'JavaScript'// that contains the fields required by interface 'Language'type JavaScript struct {DevelopedBy string}// implementing methods of interface 'Language'// for structure 'JavaScript'func (javaScript JavaScript) getDevelopedBy() string {return javaScript.DevelopedBy;}// creating a structure 'Python'// that contains the fields required by interface 'Language'type Python struct {DevelopedBy string}// implementing methods of interface 'Language'// for structure 'Python'func (python Python) getDevelopedBy() string {return python.DevelopedBy;}func main() {// creating an instance of interface 'Language'var ILanguage Language// creating an object of structure 'JavaScript'javaScript := JavaScript{DevelopedBy: "Brendan Eich"}// creating an object of structure 'Python'python := Python{DevelopedBy: "Guido van Rossum"}// assigning object 'javaScript' to 'ILanguage'// and invoking getDevelopedBy()ILanguage = javaScriptfmt.Println(ILanguage.getDevelopedBy())// assigning object 'python' to 'ILanguage'// and invoking getDevelopedBy()ILanguage = pythonfmt.Println(ILanguage.getDevelopedBy())}
Explanation
- Line 3: We import the package
fmt. - Line 6–9: We create an interface
Language. - Line 13–15: We create a structure
JavaScript. - Line 19–21: We implement the methods of the
Languageinterface for theJavaScriptstructure. - Line 25–27: We create a structure
Python. - Line 19–21: We implement the methods of the interface
Languagefor the structurePython. - Line 37: We create an instance of the interface
Language. - Line 40: We create an object of the structure
JavaScript. - Line 43: We create an object of the structure
Python. - Line 47–48: We assign the object
javaScripttoILanguageand invoke the functiongetDevelopedBy(). - Line 52–53: We assign the object
pythontoILanguageand invoke the functiongetDevelopedBy().
Output
- Here, we have created two objects of structures
JavaScriptandPython, respectively. Both implement the interfaceLanguage. - When we run the code, both these objects will invoke the function
getDevelopedBy()and will display theDevelopedByvalue on the console.
Since only one method was used for different objects, we can be sure that this is an example of polymorphism. Even if we add more types and implement the interface Language for each of them, they will be able to invoke the function getDevelopedBy() due to polymorphism.