Index
interfaces
Interfaces cannot have any functions implemented.There are further restrictions:
• They cannot inherit other contracts or interfaces.
• All declared functions must be external.
• They cannot declare a constructor.
• They cannot declare state variables.
Interfaces are denoted by their own keyword:
pragma solidity >=0.5.0 <0.7.0;
interface Token {
enum TokenType { Fungible, NonFungible }
struct Coin { string obverse; string reverse; }
function transfer(address recipient, uint amount) external;
}
interface Token {
enum TokenType { Fungible, NonFungible }
struct Coin { string obverse; string reverse; }
function transfer(address recipient, uint amount) external;
}
What is the use of a interface or function without implementation?
Take, for example, this code. The interface contains function without implementation, so how it's useful.
pragma solidity ^0.5.0;
interface Calculator {
function getResult() external view returns(uint);
}
contract Test is Calculator {
constructor() public {}
function getResult() external view returns(uint){
uint a = 1;
uint b = 2;
uint result = a + b;
return result;
}
}
interface Calculator {
function getResult() external view returns(uint);
}
contract Test is Calculator {
constructor() public {}
function getResult() external view returns(uint){
uint a = 1;
uint b = 2;
uint result = a + b;
return result;
}
}
How does this code working is different from the first one? What are the benefits of using an interface or function without implementation?
pragma solidity ^0.5.0;
contract Test {
constructor() public {}
function getResult() external view returns(uint){
uint a = 1;
uint b = 2;
uint result = a + b;
return result;
}
}
contract Test {
constructor() public {}
function getResult() external view returns(uint){
uint a = 1;
uint b = 2;
uint result = a + b;
return result;
}
}