Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Interfaces in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Interfaces
Define Interface
↓
Declare Functions (no body)
↓
Implement Interface in Contract
↓
Provide Function Bodies
↓
Use Contract via Interface Reference
↓
Call Functions as Defined
Interfaces define function signatures without code. Contracts implement these functions. Then, contracts can be used through the interface.
Execution Sample
Blockchain / Solidity
interface IToken {
  function transfer(address to, uint256 amount) external;
}

contract MyToken is IToken {
  function transfer(address to, uint256 amount) external override {
    // transfer logic
  }
}
Defines an interface IToken with a transfer function, then a contract MyToken implements it with actual code.
Execution Table
StepActionEvaluationResult
1Define interface IToken with transfer functionNo function bodyInterface created with function signature
2Define contract MyToken implementing ITokenMust implement transferContract MyToken created
3Implement transfer function in MyTokenFunction body providedMyToken now has transfer logic
4Deploy MyToken contractContract deployed on blockchainMyToken instance ready
5Call transfer via IToken interface referenceCalls MyToken.transferTransfer logic executed
6End of executionAll interface functions implemented and callableExecution complete
💡 All interface functions are implemented; contract is deployed and callable
Variable Tracker
VariableStartAfter Step 3After Step 4Final
IToken Interfaceundefineddefineddefineddefined
MyToken Contractundefineddefined (with code)deployed instancedeployed instance
transfer functionundefinedimplementedready to callready to call
Key Moments - 3 Insights
Why can't we put code inside interface functions?
Interfaces only declare function signatures without bodies, as shown in execution_table step 1, so contracts can implement them differently.
What happens if MyToken does not implement all interface functions?
The contract will not compile or deploy, because as in step 2 and 3, all interface functions must be implemented.
How do we call functions using the interface?
We use the interface type to reference the contract, then call functions as in step 5, ensuring only declared functions are accessible.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the transfer function implemented with code?
AStep 5
BStep 1
CStep 3
DStep 2
💡 Hint
Check the 'Action' and 'Result' columns at step 3 in the execution_table
According to variable_tracker, when is the MyToken contract deployed?
AAfter Step 3
BAfter Step 4
CAt Start
DAfter Final
💡 Hint
Look at the 'MyToken Contract' row and the 'After Step 4' column in variable_tracker
If we remove the transfer function implementation in MyToken, what happens?
AContract fails to compile or deploy
BContract deploys successfully
CInterface functions are optional
DInterface automatically provides default code
💡 Hint
Refer to key_moments about implementation requirements and execution_table steps 2 and 3
Concept Snapshot
Interfaces declare function signatures without code.
Contracts must implement all interface functions.
Use interface type to interact with contracts.
Interfaces ensure consistent function names and parameters.
They help separate definition from implementation.
Full Transcript
Interfaces in blockchain programming define function signatures without any code. This means they only say what functions exist and their inputs and outputs, but not how they work. Contracts then implement these interfaces by writing the actual code for each function. The flow starts by defining the interface, then creating a contract that implements it, providing function bodies. After deployment, the contract can be used through the interface reference, calling the implemented functions. This ensures that any contract following the interface has the same function names and parameters, making interaction predictable and standardized. If a contract does not implement all interface functions, it will not compile or deploy. Calling functions via the interface type restricts access to only those declared in the interface, improving safety and clarity.

Practice

(1/5)
1.

What is the main purpose of an interface in blockchain smart contracts?

easy
A. To define a set of function signatures without implementation
B. To store data variables for the contract
C. To execute transactions automatically
D. To create user interfaces for blockchain apps

Solution

  1. Step 1: Understand what an interface is

    An interface only declares function names and their inputs/outputs but does not provide code inside them.
  2. Step 2: Identify the purpose in blockchain contracts

    Interfaces ensure different contracts agree on function names and types to interact properly.
  3. Final Answer:

    To define a set of function signatures without implementation -> Option A
  4. Quick Check:

    Interface = function signatures only [OK]
Hint: Interfaces declare functions without code to ensure consistency [OK]
Common Mistakes:
  • Thinking interfaces store data variables
  • Confusing interfaces with executable code
  • Believing interfaces create UI elements
2.

Which of the following is the correct syntax to declare an interface named IPayment in Solidity?

?
easy
A. interface IPayment { function pay() external; }
B. contract IPayment { function pay() public {} }
C. interface IPayment { function pay() public {} }
D. contract IPayment { function pay() external; }

Solution

  1. Step 1: Recall interface syntax in Solidity

    Interfaces use the keyword interface and declare functions without bodies, marked external.
  2. Step 2: Check each option

    interface IPayment { function pay() external; } correctly declares an interface with an external function without implementation. Versions using contract are wrong. interface IPayment { function pay() public {} } has a function body which is not allowed in interfaces.
  3. Final Answer:

    interface IPayment { function pay() external; } -> Option A
  4. Quick Check:

    Interface + external function + no body = correct syntax [OK]
Hint: Interfaces use 'interface' keyword and external functions without bodies [OK]
Common Mistakes:
  • Using 'contract' instead of 'interface'
  • Adding function bodies inside interfaces
  • Using 'public' instead of 'external' in interface functions
3.

Given the interface and contract below, what will be the output when calling getValue()?

interface IValue {
    function getValue() external view returns (uint);
}

contract Value is IValue {
    function getValue() external pure returns (uint) {
        return 42;
    }
}
medium
A. 42
B. 0
C. Runtime error
D. Compilation error

Solution

  1. Step 1: Compare function signatures in interface and contract

    The interface declares getValue() as view, but the contract implements it as pure. These must match exactly.
  2. Step 2: Understand Solidity rules for interface implementation

    Function modifiers like view and pure must match. Mismatch causes a compilation error.
  3. Final Answer:

    Compilation error -> Option D
  4. Quick Check:

    Modifier mismatch = compilation error [OK]
Hint: Function modifiers must match exactly in interface and implementation [OK]
Common Mistakes:
  • Ignoring function modifier differences
  • Assuming pure and view are interchangeable
  • Expecting runtime error instead of compile error
4.

Identify the error in the following Solidity code implementing an interface:

interface ICalculator {
    function add(uint a, uint b) external returns (uint);
}

contract Calculator is ICalculator {
    function add(uint a, uint b) public returns (uint) {
        return a + b;
    }
}
medium
A. Contract should not use 'is' keyword
B. Function visibility mismatch between interface and contract
C. Interface cannot have functions with parameters
D. Missing function body in contract

Solution

  1. Step 1: Check function visibility in interface and contract

    The interface function is declared as external, but the contract function is public. These must match.
  2. Step 2: Understand Solidity interface implementation rules

    Function visibility must be the same; otherwise, it causes a compilation error.
  3. Final Answer:

    Function visibility mismatch between interface and contract -> Option B
  4. Quick Check:

    Visibility mismatch = compilation error [OK]
Hint: Match function visibility exactly between interface and contract [OK]
Common Mistakes:
  • Using public instead of external in implementation
  • Thinking interfaces cannot have parameters
  • Ignoring 'is' keyword for inheritance
5.

You want to create a contract Token that implements two interfaces IERC20 and IERC721. Which of the following is the correct way to declare this contract?

hard
A. interface Token is IERC20, IERC721 { /* implementation */ }
B. contract Token implements IERC20, IERC721 { /* implementation */ }
C. contract Token is IERC20, IERC721 { /* implementation */ }
D. contract Token inherits IERC20, IERC721 { /* implementation */ }

Solution

  1. Step 1: Recall Solidity syntax for multiple interface inheritance

    Solidity uses the keyword is to inherit from multiple interfaces or contracts.
  2. Step 2: Evaluate each option

    contract Token is IERC20, IERC721 { /* implementation */ } correctly uses is with both interfaces. contract Token implements IERC20, IERC721 { /* implementation */ } uses implements which is invalid in Solidity. interface Token is IERC20, IERC721 { /* implementation */ } declares Token as an interface, not a contract. contract Token inherits IERC20, IERC721 { /* implementation */ } uses inherits which is not a Solidity keyword.
  3. Final Answer:

    contract Token is IERC20, IERC721 { /* implementation */ } -> Option C
  4. Quick Check:

    Use 'is' for multiple interface inheritance [OK]
Hint: Use 'is' keyword to inherit multiple interfaces in Solidity [OK]
Common Mistakes:
  • Using 'implements' instead of 'is'
  • Declaring contract as interface by mistake
  • Using non-existent keywords like 'inherits'
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard