Interfaces of Smart Contracts in Solidity

Published by Mario Oettler on

Last Updated on 12. June 2023 by Mario Oettler

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);
    }
}
Categories:

https://blockchain-academy.hs-mittweida.de/wp-content/uploads/2021/04/logo_bcam_rgb_gross.png

Welcome

Blockchain Academy

Continue with credential

No account yet?

Powered by Hidy

Register with Hidy


Register
Sign in

We need the following credential to register:

please select:

To create a new account, we need the following data from you:

We would appreciate the following additional information from you for the creation of your account:

Welcome

Blockchain Academy

Continue with credential

Already have an account?

Powered by Hidy

Sign in with Hidy


Register
Sign in

We need the following credential to log in:

please select: