Welcome to Golang Programs, Get the best new programs in your inbox, daily.

Interface

What is an interface?

Go doesn't have classic Object Oriented concept of classes and inheritance. Interfaces in Go provide a way to specify the behavior of an object: do like this and reuse it.

In Go programming language Interfaces are types that just defines a set of methods (the method set), but these methods do not contain code. This mthods is never implemented by the interface type directly. Also an interface cannot contain variables. Go's interface type provides lot of extensibility and composability for your Go applications. Interface types express generalizations or abstractions about the behaviors of other types.

Interfaces are particularly useful as software projects grow and become more complex. They allow us to hide the incidental details of implementation the basic concept of Oops.

Declaration of Interface Types

An interface type is declared with the keyword interface.

An interface is declared in the format:

type Name interface {
	Method1(param_list) return_type
	Method2(param_list) return_type
	…
}

where Name is an interface type.

type Information interface {
	General()
	Attributes()
	Inventory()
}

The interface type Information is a contract for creating various Product types in a catalog.

The Information interface provides three behaviors in its contract: General,Attributes and Inventory.

Implemention of Interface into Concrete Types

When a userdefined type implements the set of methods declared by an interface type the values of the user-defined type can be assigned back to the values of the interface type. In this process the value of the user-defined type getting stores into the interface value.

type Product struct {
	Name, Description string
	Weight,Price float64
	Stock int
}
func (prd Product) General() {
	fmt.Printf("\n%s",prd.Name)
	fmt.Printf("\n%s\n",prd.Description)
	fmt.Println(prd.Price)
}
func (prd Product) Attributes(){
	fmt.Println(prd.Weight)
}

A struct Product is declared with fields for holding its state and methods implemented based on the behaviors defined in the Information interface.Multiple types can implement the same interface. A type that implements an interface can also have other functions. A type can implement many interfaces.

Type Embedding

Go allows you to take existing types and both extend and change their behavior. This capability is important for code reuse and for changing the behavior of an existing type to suit a new need. This is accomplished through type embedding. It works by taking an existing type and declaring that type within the declaration of a new struct type. The type that is embedded is then called an inner type of the new outer type.

A struct Mobile is declared in which the type Product is embedded.

type Mobile struct{
	Product
	DisplayFeatures []string
	ProcessorFeatures []string
}

A struct Mobile is declared in which the type Product is embedded. The type Product is an implementation of the Information interface, the type Mobile is also an implementation of the Information interface. All fields and methods defined in the Type Product types are also available in the Mobile type. In addition to the embedded type of Product, the Mobile struct provides a DisplayFeatures and ProcessorFeatures fields to represent the Attributes for Mobile type.

Method Overriding

Since any user-defined type can implement any interface, method calls against an interface value are polymorphic in nature. The userdefined type in this relationship is often called a concrete type, since interface values have no concrete behavior without the implementation of the stored user-defined value.

func (mob Mobile) Attributes(){
	mob.Product.Attributes()
	fmt.Println("\nDisplay Features:")
	for _, key := range mob.DisplayFeatures{
		fmt.Println(key)	
	}
	fmt.Println("\nProcessor Features:")
	for _, key := range mob.ProcessorFeatures{
		fmt.Println(key)	
	}
}

The Mobile struct is a concrete implementation of the Information interface than an Product type. The Product type is defined for type embedding for making a more concrete implementation of the Information interface, like the Mobile struct. At this moment, the Mobile struct uses the methods that were defined in the Product struct. Because the Mobile struct is more of a concrete implementation, it might have its own implementations for its methods. Here the Mobile struct might need to override the methods defined in the Product struct to provide extra functionalities.