{"id":7422,"date":"2024-03-21T07:45:17","date_gmt":"2024-03-21T07:45:17","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=7422"},"modified":"2024-03-21T08:06:14","modified_gmt":"2024-03-21T08:06:14","slug":"visibility-and-inheritance-in-solidity","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/visibility-and-inheritance-in-solidity\/","title":{"rendered":"<strong>Understanding Visibility and Inheritance in Solidity<\/strong>"},"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\/visibility-and-inheritance-in-solidity\/#Visibility_modifiers\" title=\"Visibility modifiers\">Visibility modifiers<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/visibility-and-inheritance-in-solidity\/#Public_functions_and_variables\" title=\"Public functions and variables\">Public functions and variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/visibility-and-inheritance-in-solidity\/#Private_functions_and_variables\" title=\"Private functions and variables\">Private functions and variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/visibility-and-inheritance-in-solidity\/#Internal_functions_and_variables\" title=\"Internal functions and variables\">Internal functions and variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/visibility-and-inheritance-in-solidity\/#External_functions_and_variables\" title=\"External functions and variables\">External functions and variables<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/metaschool.so\/articles\/visibility-and-inheritance-in-solidity\/#Difference_between_private_and_internal_functions\" title=\"Difference between private and internal functions\">Difference between private and internal functions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/metaschool.so\/articles\/visibility-and-inheritance-in-solidity\/#Difference_between_public_and_external_functions\" title=\"Difference between public and external functions\">Difference between public and external functions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/metaschool.so\/articles\/visibility-and-inheritance-in-solidity\/#Contract_inheritance\" title=\"Contract inheritance\">Contract inheritance<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/metaschool.so\/articles\/visibility-and-inheritance-in-solidity\/#Getting_modules_from_another_file\" title=\"Getting modules from another file\">Getting modules from another file<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/metaschool.so\/articles\/visibility-and-inheritance-in-solidity\/#Modules_dont_have_to_be_separate_files\" title=\"Modules don&#8217;t have to be separate files\">Modules don&#8217;t have to be separate files<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/metaschool.so\/articles\/visibility-and-inheritance-in-solidity\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>In Solidity, visibility and inheritance are pivotal concepts governing the accessibility and reusability of functions and variables within contracts. Understanding these concepts is essential for writing secure and modular smart contracts. Let&#8217;s delve deeper into each of these concepts, exploring their nuances and practical implications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"visibility-modifiers\"><span class=\"ez-toc-section\" id=\"Visibility_modifiers\"><\/span>Visibility modifiers<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Solidity provides four visibility modifiers: <code>public<\/code>, <code>private<\/code>, <code>internal<\/code>, and <code>external<\/code>. These modifiers regulate how functions and variables can be accessed within and outside the contract.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"public-functions-and-variables\"><span class=\"ez-toc-section\" id=\"Public_functions_and_variables\"><\/span><strong>Public functions and variables<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Public functions and variables can be accessed from within the contract, as well as externally via transactions and derived contracts. They are suitable for exposing essential functionalities and state variables to other contracts or external users.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>contract MyContract {\n    uint public myNumber;\n\n    function setNumber(uint _num) public {\n        myNumber = _num;\n    }\n}<\/code><\/pre>\n\n\n\n<p>Here, <code>myNumber<\/code> is a public state variable that can be accessed by any other contract or external account. The <code>setNumber<\/code> function is also public, allowing external users to update the value of <code>myNumber<\/code>.<\/p>\n\n\n\n<p><strong>\ud83d\udd25 Check this course out:<\/strong>\u00a0<a href=\"https:\/\/metaschool.so\/courses\/one-piece-personality-dapp-solidity\" target=\"_blank\" rel=\"noreferrer noopener\">Build a One Piece Personality dApp With Solidity<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"private-functions-and-variables\"><span class=\"ez-toc-section\" id=\"Private_functions_and_variables\"><\/span><strong>Private functions and variables<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Private functions and variables are only accessible within the contract they are declared in. They cannot be accessed externally or by derived contracts, enhancing encapsulation and security.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>contract MyContract {\n    uint private myNumber;\n\n    function setNumber(uint _num) private {\n        myNumber = _num;\n    }\n}<\/code><\/pre>\n\n\n\n<p>In this case, <code>myNumber<\/code> can only be modified by functions within <code>MyContract<\/code>. External contracts or transactions cannot access or modify <code>myNumber<\/code>, ensuring their integrity.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"internal-functions-and-variables\"><span class=\"ez-toc-section\" id=\"Internal_functions_and_variables\"><\/span><strong>Internal functions and variables<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Internal functions and variables are accessible within the contract and derived contracts. They provide a middle ground between public and private visibility, allowing for controlled access within the contract hierarchy.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>contract BaseContract {\n    uint internal myNumber;\n\n    function setNumber(uint _num) internal {\n        myNumber = _num;\n    }\n}\n\ncontract DerivedContract is BaseContract {\n    function updateNumber(uint _num) public {\n        setNumber(_num); \/\/ Can access internal function from BaseContract\n    }\n}<\/code><\/pre>\n\n\n\n<p>Here, <code>myNumber<\/code> and <code>setNumber()<\/code> are accessible within <code>DerivedContract<\/code> because it inherits from <code>BaseContract<\/code>. However, external contracts cannot directly access <code>setNumber<\/code>, ensuring that only contracts within the inheritance hierarchy can modify <code>myNumber<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"external-functions-and-variables\"><span class=\"ez-toc-section\" id=\"External_functions_and_variables\"><\/span><strong>External functions and variables<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>External functions can only be called externally, i.e., from other contracts or transactions. They cannot be accessed internally or by derived contracts. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>contract MyContract {\n    function myFunction() external {\n        \/\/ Code implementation\n    }\n}<\/code><\/pre>\n\n\n\n<p>The <code>myFunction<\/code> can only be called by other contracts or through transactions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"difference-between-private-and-internal-functions\"><span class=\"ez-toc-section\" id=\"Difference_between_private_and_internal_functions\"><\/span>Difference between private and internal functions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The main difference between <code>private<\/code> and <code>internal<\/code> functions lie in their visibility scope:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Private Functions<\/strong>: Private functions are only accessible within the contract they are declared in. They cannot be accessed externally or by derived contracts. Private functions are suitable for implementing internal logic or utility functions that should not be exposed outside the contract.<\/li><li><strong>Internal Functions<\/strong>: Internal functions are accessible within the contract and derived contracts. While they cannot be accessed externally, they provide a way for derived contracts to access and override their behavior. Internal functions are useful for implementing reusable logic that can be customized by derived contracts.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"difference-between-public-and-external-functions\"><span class=\"ez-toc-section\" id=\"Difference_between_public_and_external_functions\"><\/span>Difference between public and external functions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>While both <code>public<\/code> and <code>external<\/code> functions can be called from outside the contract, they have distinct differences:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Public Functions<\/strong>: Public functions can be called both internally and externally. They are accessible from within the contract, derived contracts, and externally via transactions.<\/li><li><strong>External Functions<\/strong>: External functions can only be called externally, i.e., from other contracts or transactions. They cannot be accessed internally or by derived contracts.<\/li><\/ul>\n\n\n\n<p>\ud83d\udd25\u00a0<strong>Check this course out:<\/strong>\u00a0<a href=\"https:\/\/metaschool.so\/courses\/how-to-write-a-smart-contract-and-mint-elon-musk-nft-on-opensea\" target=\"_blank\" rel=\"noreferrer noopener\">Write an\u00a0Elon Musk NFT Smart Contract on OpenSea<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"contract-inheritance\"><span class=\"ez-toc-section\" id=\"Contract_inheritance\"><\/span>Contract inheritance<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Inheritance enables contracts to inherit properties and functionalities from other contracts, fostering code reuse and modularity. Solidity supports both inheriting from contracts defined in separate files and combining multiple contracts within a single file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"getting-modules-from-another-file\"><span class=\"ez-toc-section\" id=\"Getting_modules_from_another_file\"><\/span><strong>Getting modules from another file<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In Solidity, you can import external contracts into your file and inherit from them using the <code>is<\/code> keyword. This approach allows for better organization of code and facilitates the reuse of existing functionalities.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>\/\/ File: Animal.sol\ncontract Animal {\n    string internal _sound;\n\n    constructor(string memory sound) {\n        _sound = sound;\n    }\n\n    function makeSound() public view returns (string memory) {\n        return _sound;\n    }\n}\n\n\/\/ File: Dog.sol\nimport \".\/Animal.sol\";\n\ncontract Dog is Animal {\n    constructor() Animal(\"Woof\") {}\n}<\/code><\/pre>\n\n\n\n<p>Here, the <code>Dog<\/code> contract inherits from the <code>Animal<\/code> contract, importing it from another file (<code>Animal.sol<\/code>). This enables <code>Dog<\/code> to access the functionalities defined in <code>Animal<\/code>, such as the <code>makeSound()<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"modules-don-t-have-to-be-separate-files\"><span class=\"ez-toc-section\" id=\"Modules_dont_have_to_be_separate_files\"><\/span><strong>Modules don&#8217;t have to be separate files<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Solidity also allows you to define multiple contracts within a single file and inherit from them internally. This approach simplifies project structure and reduces the overhead of managing multiple files.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>contract Animal {\n    string internal _sound;\n\n    constructor(string memory sound) {\n        _sound = sound;\n    }\n\n    function makeSound() public view returns (string memory) {\n        return _sound;\n    }\n}\n\ncontract Dog is Animal {\n    constructor() Animal(\"Woof\") {}\n}<\/code><\/pre>\n\n\n\n<p>In this example, both <code>Animal<\/code> and <code>Dog<\/code> contracts are defined within the same file. Despite not being in separate files, <code>Dog<\/code> still inherits from <code>Animal<\/code>, allowing it to access <code>Animal<\/code>&#8216;s functionalities.<\/p>\n\n\n\n<p><strong>\ud83d\udd25 Check this course out:<\/strong>\u00a0<a href=\"https:\/\/metaschool.so\/courses\/create-your-own-ethereum-token-in-just-30-mins\" target=\"_blank\" rel=\"noreferrer noopener\">Create Your Own Ethereum Token in Just 30 Mins<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Mastering visibility and inheritance in Solidity is essential for building robust and modular smart contracts. By understanding how visibility modifiers regulate access to functions and variables, you can ensure security and encapsulation within your contracts. Moreover, leveraging contract inheritance allows for efficient code reuse and promotes a modular design approach.<\/p>\n\n\n\n<p>In summary, consider the following key points:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Visibility modifiers (<code>public<\/code>, <code>private<\/code>, <code>internal<\/code>) regulate access to functions and variables within contracts.<\/li><li>Contract inheritance enables the reuse of existing functionalities and promotes code modularity.<\/li><li>Solidity supports both importing external contracts and defining multiple contracts within a single file for inheritance purposes.<\/li><\/ul>\n\n\n\n<p>By incorporating these concepts into your Solidity projects, you can write more efficient, maintainable, and scalable smart contracts for decentralized applications. Happy coding!<\/p>\n\n\n\n<p><strong>Try it out, ask us questions, and tell us how it went by tagging Metaschool on Social Media.<\/strong><\/p>\n\n\n\n<p><strong>Follow us on<\/strong>&nbsp;\u2013<\/p>\n\n\n\n<p>\ud83d\udd2eTwitter \u2013\u00a0<a href=\"https:\/\/twitter.com\/0xmetaschool\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/twitter.com\/0xmetaschool<\/a><\/p>\n\n\n\n<p>\ud83d\udd17LinkedIn \u2013\u00a0<a href=\"https:\/\/www.linkedin.com\/company\/0xmetaschool\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.linkedin.com\/company\/0xmetaschool\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":15,"featured_media":7423,"comment_status":"closed","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":[51,46,29],"class_list":["post-7422","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-blockchain","tag-solidity","tag-web3"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7422","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\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=7422"}],"version-history":[{"count":3,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7422\/revisions"}],"predecessor-version":[{"id":7436,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7422\/revisions\/7436"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/7423"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=7422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=7422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=7422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}