Welcome to the lesson on Polymorphism with Go. Polymorphism, an essential pillar of object-oriented programming (OOP), enables us to design versatile, legible, and modular code that promotes code reuse. In this lesson, you will understand Polymorphism and its implementation in Go.
The marvel of Polymorphism lies in its ability to treat different data types as identical forms, thus simplifying your code and boosting its extensibility.
Consider the management of a zoo that houses various animals, such as Tigers, Elephants, and Monkeys. Each animal type exhibits specific behaviors but shares common features like eating, thereby demonstrating Polymorphism.
Polymorphism in Go is implemented using Interfaces — a sophisticated collection of method signatures that allow different entities to follow standard interactions.
We demonstrate this with an example featuring the Shape
interface and two structs — Circle
and Rectangle
.
Go1package main 2 3import ( 4 "fmt" 5 "math" 6) 7 8// Shape interface declaration 9type Shape interface { 10 Area() float64 11} 12 13// Circle struct declaration 14type Circle struct { 15 radius float64 16} 17 18// Rectangle struct declaration 19type Rectangle struct { 20 width, height float64 21} 22 23// Implementing the Area method for Circle 24func (c Circle) Area() float64 { 25 return math.Pi * c.radius * c.radius 26} 27 28// Implementing the Area method for Rectangle 29func (r Rectangle) Area() float64 { 30 return r.width * r.height 31} 32 33func main() { 34 circle := Circle{2} 35 rectangle := Rectangle{3, 4} 36 fmt.Printf("Area of Circle: %f\n", circle.Area()) //prints "Area of Circle: 12.566371" 37 fmt.Printf("Area of Rectangle: %f\n", rectangle.Area()) //prints "Area of Rectangle: 12.000000" 38}
Here, both Circle
and Rectangle
implement the Area
method. Thus, wherever a Shape
is expected, a Circle
or Rectangle
can be used interchangeably.
Let's exemplify Polymorphism using a zoo simulator that houses Dogs, Cats, and Birds. We aim to hear each animal's unique sound.
Go1package main 2 3import ( 4 "fmt" 5) 6 7// Animal interface declaration 8type Animal interface { 9 Sound() string 10} 11 12// Dog struct declaration 13type Dog struct{} 14 15// Cat struct declaration 16type Cat struct{} 17 18// Bird struct declaration 19type Bird struct{} 20 21// Implementing the Sound method for Dog 22func (d Dog) Sound() string { 23 return "Woof!" 24} 25 26// Implementing the Sound method for Cat 27func (c Cat) Sound() string { 28 return "Meow!" 29} 30 31// Implementing the Sound method for Bird 32func (b Bird) Sound() string { 33 return "Tweet!" 34} 35 36func makeSound(a Animal) { 37 fmt.Println(a.Sound()) 38} 39 40func main() { 41 dog := Dog{} 42 cat := Cat{} 43 bird := Bird{} 44 45 makeSound(dog) //prints "Woof!" 46 makeSound(cat) //prints "Meow!" 47 makeSound(bird) //prints "Tweet!" 48}
Here, the Dog
, Cat
, and Bird
structs implement the Animal
interface. This exemplifies Polymorphism, as makeSound
works with any Animal
and prints the animal's unique sound.
In this lesson, we delved into Polymorphism — an indispensable component of enhancing code flexibility and efficiency. The Interfaces in Go offer a robust mechanism to implement Polymorphism; understanding and using it enhances your programming capabilities.
Now, strengthen your understanding of Polymorphism through relatable real-world scenarios.
Having unveiled the concept of Polymorphism, it's now time to put theory into practice. After the lesson, engage with tailor-made exercises to apply the freshly-gained knowledge. Remember, practice sessions unlock diverse insights into Polymorphism. Embrace the fun of coding with Go!