ReasonJun

Solidity : Difference between 'abstract' and 'interface' 본문

Blockchain/Solidity

Solidity : Difference between 'abstract' and 'interface'

ReasonJun 2023. 10. 17. 15:08
728x90

Abstract contracts and interfaces are both powerful features of Solidity that can be used to improve the quality, flexibility, and readability of your code. However, there are some key differences between the two.

 

Abstract contracts

  • Abstract contracts are contracts that cannot be deployed by themselves. They must be inherited by other contracts.
  • Abstract contracts are used to define the common functionality of a group of related contracts.
  • Abstract contracts can contain function declarations, as well as state variables and other code.
  • Abstract contracts can be used to implement a variety of design patterns, such as the template method pattern and the strategy pattern.

Interfaces

  • Interfaces are contracts that can only contain function declarations.
  • Interfaces are used to define the common functionality of a group of related contracts.
  • Interfaces cannot contain state variables or other code.
  • Interfaces are used to create libraries of reusable functionality and to define the requirements for contracts that you want to interact with.

Here is a table that summarizes the key differences between abstract contracts and interfaces:

Feature Abstract contract Interface
Can be deployed? No No
Must be inherited? Yes No
Can contain state variables? Yes No
Can contain other code? Yes No

When to use abstract contracts

  • Use abstract contracts to define the common functionality of a group of related contracts.
  • Use abstract contracts to implement design patterns.

When to use interfaces

  • Use interfaces to define the requirements for contracts that you want to interact with.
  • Use interfaces to create libraries of reusable functionality.
728x90
Comments