What is Solidity interface?

What is an interface?

Interfaces can be seen as pure abstract contracts.

Key points about solidity interface

  • Functions can not be implemented.
  • Interfaces can be inherited.
  • External functions must be defined for all stated functions.
  • Constructors can not be declared, state variables can’t also be declared.

Example code

pragma solidity ^0.5.0;
interface Multiply {
function getOutput() external view returns(uint);
}
contract sample is Multiply {
function getOutput() external view returns(uint){
uint x = 12;
uint y = 10;
uint multval = x * y;
return multval;
}
}

Explanation

  • Lines 3 to 5: We instantiated our multiply interface.

  • Line 6: We created a contracted and implemented the created interface.

  • Line 7: We are utilizing the getOutput() function.

  • Lines 8 to 9: We are declaring and defining variables.

  • Line 10: We carry out the logic for multiplication and equate it to multval variable

  • Line 11: We return the multiplied value.

Free Resources