Interfaces of Smart Contracts in Solidity
Interfaces are similar to abstract contracts, but they have no function implemented. Interfaces help you to design larger dApps.
Interfaces face certain restrictions:
- Must not have any function with implementation.
- Functions of an interface must be external.
- Interface cannot have a constructor.
- Interface cannot have state variables.
- Interface can have enum, structs which can be accessed using interface name dot notation.
Interfaces are basically limited to what a contract ABI can represent. It is possible to convert an ABI to an interface and vice versa without loss of information.
Implementation of Interfaces
Interfaces are declared with the keyword interface. Contracts using an interface include it with the keyword is.
pragma solidity 0.8.20;
interface SumInterface{
struct Task{
uint256 a;
uint256 b;
uint256 result;
}
enum Difficulty {difficult, easy}
function sum(uint256, uint256) external returns(uint256);
}
contract Calculator is SumInterface{
function sum(uint256 _a, uint256 _b) override public pure returns(uint256 result){
return (_a + _b);
}
}