{"id":3688,"date":"2022-12-15T13:27:07","date_gmt":"2022-12-15T13:27:07","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=3688"},"modified":"2023-08-31T10:27:17","modified_gmt":"2023-08-31T10:27:17","slug":"solidity-interface-what-is-it","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/solidity-interface-what-is-it\/","title":{"rendered":"What is a Solidity interface? Explained with examples"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/solidity-interface-what-is-it\/#What_is_it\" title=\"What is it?\">What is it?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/solidity-interface-what-is-it\/#How_to_implement_an_interface_in_a_Solidity_contract\" title=\"How to implement an interface in a Solidity contract\">How to implement an interface in a Solidity contract<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>In <a href=\"https:\/\/metaschool.so\/solidity?utm_campaign=smf&amp;utm_source=blog&amp;utm_medium=organic\" target=\"_blank\" rel=\"noreferrer noopener\">Solidity<\/a>, an interface is a special type of contract that defines a set of functions that other contracts can implement. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-it\"><span class=\"ez-toc-section\" id=\"What_is_it\"><\/span>What is it?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>An interface defines the signatures of the functions, but not their implementations. This means that an interface specifies what a contract must do, but not how it does it. <\/p>\n\n\n\n<p>Interfaces are useful because they allow contracts to be written in a modular and reusable way. By defining a Solidity interface, you can specify a contract&#8217;s external behavior and allow other contracts to implement that behavior in any way they choose. This makes it possible to <a href=\"https:\/\/metaschool.so\/courses\/writing-your-first-hello-world-contract-in-solidity?ref=Articles&amp;utm_source=Blog_Organic\" target=\"_blank\" rel=\"noreferrer noopener\">write smart contracts<\/a> that can be used in different contexts and can be easily extended or modified.<\/p>\n\n\n\n<p>Here is an example of an interface in Solidity:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\"><code>interface Token {\n    function transfer(address _to, uint256 _value) external;\n    function balanceOf(address _owner) external view returns (uint256);\n}<\/code><\/pre>\n\n\n\n<p>In this example, the <code>Token<\/code> interface defines two functions: <code>transfer<\/code> and <code>balanceOf<\/code>. These functions have specific signatures, which means that they have specific parameter types and return types. The <code>transfer<\/code> function takes two arguments: an <code>address<\/code> and a <code>uint256<\/code>. The <code>balanceOf<\/code> function takes one argument: an <code>address<\/code>. Both functions also have the <code>external<\/code> visibility modifier, which means that they can be called from outside the contract.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-implement-an-interface-in-a-solidity-contract\"><span class=\"ez-toc-section\" id=\"How_to_implement_an_interface_in_a_Solidity_contract\"><\/span>How to implement an interface in a Solidity contract<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To implement an interface in a contract, you can use the <code>is<\/code> keyword followed by the interface name. Here is an example of a smart contract that implements the <code>Token<\/code> interface:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-nv-site-bg-background-color has-background\">Copy code<code>contract MyToken is Token {\n    function transfer(address _to, uint256 _value) external {\n        \/\/ Implement the transfer function here\n    }\n\n    function balanceOf(address _owner) external view returns (uint256) {\n        \/\/ Implement the balanceOf function here\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>In this example, the <code>MyToken<\/code> contract implements the <code>Token<\/code> interface by defining <a href=\"https:\/\/www.tutorialspoint.com\/computer_programming\/computer_programming_functions.htm\" target=\"_blank\" rel=\"noreferrer noopener\">functions<\/a> with the same names and signatures as the functions in the interface. This means that the <code>MyToken<\/code> contract can be used in any context where a <code>Token<\/code> contract is expected, because it implements the same external behavior as defined by the interface.<\/p>\n\n\n\n<p>To conclude, an interface in Solidity is a special type of contract that defines a set of functions that other contracts can implement. Interfaces allow contracts to be written in a modular and reusable way, and they are useful for specifying a contract&#8217;s external behavior. To implement an interface in a contract, you can use the <code>is<\/code> keyword followed by the interface name.<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":3689,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[292],"tags":[31,46],"class_list":["post-3688","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-ethereum-blockchain","tag-solidity"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=3688"}],"version-history":[{"count":5,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3688\/revisions"}],"predecessor-version":[{"id":6823,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/3688\/revisions\/6823"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/3689"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=3688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=3688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=3688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}