- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Polymorphism Using Interfaces in Golang
Polymorphism is a powerful concept in object-oriented programming that allows you to write flexible and reusable code. In Go, you can achieve polymorphism using interfaces. An interface is a collection of method signatures that any type can implement. This means that you can write code that can work with any type that implements an interface, without knowing the underlying type.
In this article, we will discuss how to achieve polymorphism using interfaces in Golang.
What is Polymorphism?
Polymorphism is the ability of an object to take on many forms. In object-oriented programming, polymorphism is achieved when an object can be treated as an instance of its own class or as an instance of its parent class. This means that a single method can have different implementations in different classes.
For example, consider a program that needs to print the area of different shapes. We can create a Shape interface with an Area() method that calculates the area of a shape. We can then create different types of shapes such as Circle, Square, and Rectangle that implement the Shape interface and provide their own implementation of the Area() method. We can then write a function that takes a Shape interface and calls the Area() method to print the area of the shape.
Polymorphism Using Interfaces in Golang
In Go, polymorphism can be achieved using interfaces. An interface is a collection of method signatures that any type can implement. This means that you can write code that can work with any type that implements an interface, without knowing the underlying type.
Example
Let's take an example to understand how to achieve polymorphism using interfaces in Go −
package main import ( "fmt" "math" ) type Shape interface { Area() float64 } type Circle struct { Radius float64 } type Square struct { Length float64 } func (c Circle) Area() float64 { return math.Pi * c.Radius * c.Radius } func (s Square) Area() float64 { return s.Length * s.Length } func PrintArea(s Shape) { fmt.Println("Area of shape is:", s.Area()) } func main() { c := Circle{Radius: 5} s := Square{Length: 4} PrintArea(c) PrintArea(s) }
Output
Area of shape is: 78.53981633974483 Area of shape is: 16
In the above example, we have created a Shape interface with an Area() method that calculates the area of a shape. We have then created two different types of shapes: Circle and Square, that implement the Shape interface and provide their own implementation of the Area() method.
We have also defined a PrintArea() function that takes a Shape interface and calls the Area() method to print the area of the shape.
In the main function, we have created a Circle and a Square and passed them to the PrintArea() function. The PrintArea() function works with any type that implements the Shape interface, so it can work with both Circle and Square.
Conclusion
In this article, we discussed how to achieve polymorphism using interfaces in Go. We learned that an interface is a collection of method signatures that any type can implement, allowing us to write code that can work with any type that implements an interface, without knowing the underlying type. We also saw an example of how to use interfaces to achieve polymorphism in Go.