Embedding Interfaces in Golang


In object-oriented programming, the concept of inheritance allows the creation of a new class that is a modified version of an existing class, inheriting the properties and methods of the base class. Golang doesn't support traditional inheritance, but it provides the concept of interface embedding, which is a powerful way of reusing code.

What is Interface Embedding?

Interface embedding is a way of composing interfaces by combining two or more interfaces into a single interface. It allows you to reuse the methods of one interface in another interface without having to redefine them. It also provides a way of extending interfaces without breaking existing code.

Example

Let's take an example to understand interface embedding in Golang. Suppose we have two interfaces: Shape and Color. We want to create a new interface named ColoredShape, which should have all the methods of Shape and Color interfaces.

type Shape interface {
   Area() float64
}

type Color interface {
   Fill() string
}

type ColoredShape interface {
   Shape
   Color
}

In the above code, we created three interfaces: Shape, Color, and ColoredShape. ColoredShape interface is created by embedding Shape and Color interfaces. It means that ColoredShape interface will have all the methods of Shape and Color interfaces.

Now, let's create a struct named Square that implements the Shape interface and a struct named Red that implements the Color interface.

type Square struct {
   Length float64
}

func (s Square) Area() float64 {
   return s.Length * s.Length
}

type Red struct{}

func (c Red) Fill() string {
   return "red"
}

In the above code, we created two structs: Square and Red. Square struct implements the Shape interface by defining the Area method, and Red struct implements the Color interface by defining the Fill method.

Now, let's create a struct named RedSquare that implements the ColoredShape interface. We can do this by embedding the Shape and Color interfaces in the RedSquare struct.

type RedSquare struct {
   Square
   Red
}

func main() {
   r := RedSquare{Square{Length: 5}, Red{}}
   fmt.Println("Area of square:", r.Area())
   fmt.Println("Color of square:", r.Fill())
}

In the above code, we created a struct named RedSquare, which embeds the Square and Red structs. RedSquare struct implements the ColoredShape interface, which has all the methods of Shape and Color interfaces. We can access the Area method of the Shape interface and the Fill method of the Color interface using the RedSquare struct.

Example 

Here's an example code snippet that demonstrates embedding interfaces in Go −

package main

import "fmt"

// Shape interface
type Shape interface {
   area() float64
}

// Rectangle struct
type Rectangle struct {
   length, width float64
}

// Circle struct
type Circle struct {
   radius float64
}

// Embedding Shape interface in Rectangle struct
func (r Rectangle) area() float64 {
   return r.length * r.width
}

// Embedding Shape interface in Circle struct
func (c Circle) area() float64 {
   return 3.14 * c.radius * c.radius
}

func main() {
   // Creating objects of Rectangle and Circle
   r := Rectangle{length: 5, width: 3}
   c := Circle{radius: 4}

   // Creating slice of Shape interface type and adding objects to it
   shapes := []Shape{r, c}

   // Iterating over slice and calling area method on each object
   for _, shape := range shapes {
      fmt.Println(shape.area())
   }
}

Output

15
50.24

This code defines an interface Shape with a method area(). Two structs, Rectangle and Circle, are defined and they both embed the Shape interface by implementing the area() method. The main() function creates objects of Rectangle and Circle, adds them to a slice of Shape interface type, and iterates over the slice calling the area() method on each object.

Conclusion

Interface embedding is a powerful feature of Golang that allows you to compose interfaces by combining two or more interfaces into a single interface. It provides a way of reusing code and extending interfaces without breaking existing code. By embedding interfaces, you can create new interfaces that have all the methods of the embedded interfaces.

Updated on: 12-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements