Loading...

What is interface in Solidity and how to use it – Complete Guide

What is interface in Solidity and how to use it – Complete Guide

Hello Developers ?,
Hope you all are doing well, in this article we will discuss the interface in solidity. If you have an idea about abstract contracts, interfaces are a more restricted form of abstract contracts. Let’s see what are interfaces and how we use them in solidity.

What is Interface?

The concept of interface exists in many programming languages.
The point of these interfaces is to separate the declaration of the function from the actual behavior or the definition of the function. In solidity, interface acts as the contract or an agreement between itself and any contract that implements it.

But what does that mean?

It means if you are using the interface of the contract, now you are bound to use the functions declared in the interface inside your contract. So it is a kind of agreement between the interface and the contract which will be implementing the functions declared in the interface.

Basically, the interface is the skeleton of the smart contract which gives a rough idea to the other smart contracts to build the functionalities and implement them inside their contract.

Why do we use Interfaces?

The basic use of the interface is to interact with the already deployed smart contracts on the blockchain.
Let’s take an example of the ERC20 token standard contract. Openzeppelin has an ERC20 interface available so that anybody can use it to customize it according to the specification inside their contracts to create an ERC20 token.
By creating an interface, you provide a skeleton of the contract using which others can upgrade the contracts as per their features and functionalities but they have to customize the things on a given skeleton, so they are bound to use the declared function in the interface contract in a way.
Let’s say you want to inherit some functions from the other contract but you do not have access to the code of that contract in this case, we use an interface to call other contracts.

Interfaces Restrictions 

So as I mentioned in the introduction, interfaces in solidity are a more restricted form of abstract contracts. Like in abstract contracts we must have at least one function without its implementation, interface adds some more restrictions to that.

Following are the constraints for the interface to use :

  1. You can not declare state variables in the interface
  2. You can not use the constructor inside and interface
  3. You are also not allowed to write modifiers in the interface
  4. You can only declare the functions inside the interface and can not define them inside the interface.
  5. All declared functions inside the interface must be external.

How to create the interface in Solidity?

You can create an interface in solidity using the interface keyword. The interface contract includes the list of declared functions ending with the semicolon and not the curly braces like in regular contracts.

We created an interface IPerson, we usually prefix the name of the interface contract with ‘ I ‘. 

Then we declared all the functions inside the interface and made them external and then put the semicolons at the end of the function. 

In the same way, you can create any other interface. You can use  Codedamn’s Solidity Playground for practice.

Resources 

Where can you learn more about the interface in solidity?

The best way to learn solidity concepts is to follow the official documentation of solidity. The official documentation of solidity is always to date resource to check out any concept. 

Besides official documentation, you can also check out the Master Solidity course by Codedamn in which the concept of interface is covered in depth.

Also, you can use Codedamn’s Solidity Playground to practice solidity code which is free to use. 

Conclusion

The main motive of the interfaces is to make smart contracts customizable and reusable. In this article, we took a brief look into interfaces in solidity. The concept of interface is pretty much the same as in other programming languages but the implementation differs.
I hope you understood the use of the interface in solidity.

Thanks for reading!

Sharing is caring

Did you like what Shashank Wangkar wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far

Curious about this topic? Continue your journey with these coding courses: