Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface inherit from other interface in golang

Tags:

oop

go

Interface inheritance looks like the following in C#:

interface IA{
    void MethodX();
}

interface IB : IA{
    void MethodY();
}

How can I reuse interface definition in go?

like image 893
Mohsen Avatar asked Sep 01 '15 04:09

Mohsen


People also ask

Can interface inherit from different interface?

Interfaces can inherit from one or more interfaces. The derived interface inherits the members from its base interfaces. A class that implements a derived interface must implement all members in the derived interface, including all members of the derived interface's base interfaces.

Can interface implement another interface GoLang?

One interface can only extend other interface but cannot implement the other interface as the interface can only have method declarations and not the method body itself. When we say implements, that basically means to provide the method definition for the abstract methods.

How do you inherit an interface into another interface?

C# allows the user to inherit one interface into another interface. When a class implements the inherited interface then it must provide the implementation of all the members that are defined within the interface inheritance chain.

Does GoLang support multiple inheritance?

Since Golang does not support classes, so inheritance takes place through struct embedding. We cannot directly extend structs but rather use a concept called composition where the struct is used to form other objects. So, you can say there is No Inheritance Concept in Golang.


1 Answers

You can embed other interfaces inside an interface, which gives you basicaly the same benefits:

A Good Example is the ReadWriteCloser in the io package: http://golang.org/pkg/io/#ReadWriteCloser

It embeds a Reader, a Writer and a Closer interface.

like image 93
Adam Vincze Avatar answered Sep 19 '22 05:09

Adam Vincze