{"id":7326,"date":"2024-03-19T08:31:51","date_gmt":"2024-03-19T08:31:51","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=7326"},"modified":"2024-03-19T09:22:41","modified_gmt":"2024-03-19T09:22:41","slug":"internal-private-specifiers-in-solidity","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/internal-private-specifiers-in-solidity\/","title":{"rendered":"What Is the Difference Between Internal and Private Specifiers in Solidity?"},"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\/internal-private-specifiers-in-solidity\/#Understanding_visibility_specifiers_in_Solidity\" title=\"Understanding visibility specifiers in Solidity\">Understanding visibility specifiers in Solidity<\/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\/internal-private-specifiers-in-solidity\/#1_Public_visibility\" title=\"1. Public visibility\">1. Public visibility<\/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\/internal-private-specifiers-in-solidity\/#2_Private_visibility\" title=\"2. Private visibility\">2. Private visibility<\/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\/internal-private-specifiers-in-solidity\/#3_Internal_visibility\" title=\"3. Internal visibility\">3. Internal visibility<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/internal-private-specifiers-in-solidity\/#The_difference_between_internal_and_private\" title=\"The difference between internal and private\">The difference between internal and private<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/metaschool.so\/articles\/internal-private-specifiers-in-solidity\/#1_Private_The_room_locked_inside\" title=\"1. Private: The room locked inside\">1. Private: The room locked inside<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/metaschool.so\/articles\/internal-private-specifiers-in-solidity\/#2_Internal_The_family_room\" title=\"2. Internal: The family room\">2. Internal: The family room<\/a><\/li><\/ul><\/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\/internal-private-specifiers-in-solidity\/#Private_visibility_specifier_example\" title=\"Private visibility specifier example\">Private visibility specifier example<\/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\/internal-private-specifiers-in-solidity\/#Explanation\" title=\"Explanation\">Explanation<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/metaschool.so\/articles\/internal-private-specifiers-in-solidity\/#Internal_visibility_specifier_example\" title=\"Internal visibility specifier example\">Internal visibility specifier example<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/metaschool.so\/articles\/internal-private-specifiers-in-solidity\/#Explanation-2\" title=\"Explanation\">Explanation<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/metaschool.so\/articles\/internal-private-specifiers-in-solidity\/#Combined_example\" title=\"Combined example\">Combined example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/metaschool.so\/articles\/internal-private-specifiers-in-solidity\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<h2 class=\"wp-block-heading\" id=\"understanding-visibility-specifiers-in-solidity\"><span class=\"ez-toc-section\" id=\"Understanding_visibility_specifiers_in_Solidity\"><\/span>Understanding visibility specifiers in Solidity<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In Solidity, visibility specifiers are crucial for controlling access to functions and variables within smart contracts. They determine who can access these elements and from where.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-public-visibility\"><span class=\"ez-toc-section\" id=\"1_Public_visibility\"><\/span>1. <strong>Public visibility<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Public functions or variables can be accessed from outside the contract as well as from within.<\/li><li>These are commonly used for functions that need to be accessible externally, such as entry points to the contract.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-private-visibility\"><span class=\"ez-toc-section\" id=\"2_Private_visibility\"><\/span>2. <strong>Private visibility<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Private functions or variables are only accessible from within the contract that defines them.<\/li><li>They cannot be accessed from outside the contract or from derived contracts.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-internal-visibility\"><span class=\"ez-toc-section\" id=\"3_Internal_visibility\"><\/span>3. <strong>Internal visibility<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Internal functions or variables are accessible from within the contract as well as from derived contracts.<\/li><li>They cannot be accessed externally, i.e., outside the contract hierarchy.<\/li><\/ul>\n\n\n\n<p><strong>\ud83d\udd25 Check this course out:<\/strong>\u00a0<a href=\"https:\/\/metaschool.so\/courses\/build-hogwarts-sorting-cap-dapp-on-polygon-mumbai\" target=\"_blank\" rel=\"noreferrer noopener\">Build Hogwarts Sorting Hat dApp on Polygon<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-difference-between-internal-and-private\"><span class=\"ez-toc-section\" id=\"The_difference_between_internal_and_private\"><\/span>The difference between internal and private<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To understand the difference between internal and private specifiers, let&#8217;s consider a metaphor of a house and its rooms:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-private-the-room-locked-inside\"><span class=\"ez-toc-section\" id=\"1_Private_The_room_locked_inside\"><\/span>1. <strong>Private: The room locked inside<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>When a function or variable is marked as private, it&#8217;s like a room in your house that has a lock on the door.<\/li><li>Only you, the owner of the house (or the contract), can access this room. It&#8217;s entirely hidden from the outside world.<\/li><li>Even if your friends (or derived contracts) are visiting your house, they can&#8217;t enter this room because it&#8217;s locked and exclusive to you.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-internal-the-family-room\"><span class=\"ez-toc-section\" id=\"2_Internal_The_family_room\"><\/span>2. <strong>Internal: The family room<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Internal functions or variables, on the other hand, are like the family room in your house.<\/li><li>This room is accessible to all members of your family (or derived contracts). They can freely enter and use the facilities inside.<\/li><li>However, outsiders (external contracts) cannot access this room. It&#8217;s limited to the family (or derived contracts) only.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"private-visibility-specifier-example\"><span class=\"ez-toc-section\" id=\"Private_visibility_specifier_example\"><\/span><strong>Private visibility specifier example<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here\u2019s the coding example for the private visibility specifier.<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\ncontract House {\n    uint internal myInternalNumber = 20;\n    \n    function getInternalNumber() public view returns(uint) {\n        return myInternalNumber;\n    }\n}\n\ncontract MyDerivedHouse is House {\n    function getInternalNumberFromDerived() public view returns(uint) {\n        \/\/ Can access the internal variable from the parent contract\n        return myInternalNumber;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"explanation\"><span class=\"ez-toc-section\" id=\"Explanation\"><\/span>Explanation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>In this example, <code>myInternalNumber<\/code> is declared as internal in the <code>House<\/code> contract.<\/li><li>The <code>getInternalNumber()<\/code> function allows external access to retrieve the value of <code>myInternalNumber<\/code>.<\/li><li><code>MyDerivedHouse<\/code> is a contract that inherits from <code>House<\/code>.<\/li><li>The <code>getInternalNumberFromDerived()<\/code> function demonstrates how the internal variable from the parent contract can be accessed within the derived contract.<\/li><\/ul>\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<h2 class=\"wp-block-heading\" id=\"internal-visibility-specifier-example\"><span class=\"ez-toc-section\" id=\"Internal_visibility_specifier_example\"><\/span><strong>Internal visibility specifier example<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here\u2019s the coding example for the internal visibility specifier.<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\ncontract House {\n    uint private myPrivateNumber = 10;\n    \n    function getPrivateNumber() public view returns(uint) {\n        return myPrivateNumber;\n    }\n}\n\ncontract ExternalContract {\n    House private houseInstance;\n    \n    constructor(address _houseAddress) {\n        houseInstance = House(_houseAddress);\n    }\n    \n    function tryAccessPrivate() public view returns(uint) {\n        \/\/ Cannot access the private variable directly\n        \/\/ Results in a compilation error\n        \/\/ return houseInstance.myPrivateNumber;\n        \n        \/\/ Accessing via public getter function\n        return houseInstance.getPrivateNumber();\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"explanation\"><span class=\"ez-toc-section\" id=\"Explanation-2\"><\/span>Explanation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Here, <code>myPrivateNumber<\/code> is declared as private in the <code>House<\/code> contract, making it inaccessible from outside the contract.<\/li><li>The <code>getPrivateNumber()<\/code> function provides a way to access the private variable&#8217;s value externally.<\/li><li><code>ExternalContract<\/code> demonstrates an attempt to directly access the private variable, which results in a compilation error.<\/li><li>Accessing the private variable indirectly through the public getter function (<code>getPrivateNumber()<\/code>) is the proper way to retrieve its value.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"combined-example\"><span class=\"ez-toc-section\" id=\"Combined_example\"><\/span>Combined example<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let&#8217;s illustrate these concepts with Solidity code:<\/p>\n\n\n\n<pre class=\"wp-block-code has-nv-site-bg-background-color has-background\" style=\"font-size:18px\"><code>\/\/ SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\ncontract House {\n    uint private myPrivateNumber = 10;\n    uint internal myInternalNumber = 20;\n\n    function getPrivateNumber() public view returns(uint) {\n        return myPrivateNumber;\n    }\n\n    function getInternalNumber() public view returns(uint) {\n        return myInternalNumber;\n    }\n}\n\ncontract MyDerivedHouse is House {\n    function getNumbers() public view returns(uint) {\n        \/\/ Can access internal variable only from the parent contract\n        \/\/ as it's derived from it.\n        return (myInternalNumber);\n    }\n}<\/code><\/pre>\n\n\n\n<p>In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>myPrivateNumber<\/code> is private to the <code>House<\/code> contract. It cannot be accessed directly from <code>MyDerivedHouse<\/code>.<\/li><li><code>myInternalNumber<\/code> is accessible within the <code>House<\/code> contract and its derived contracts, such as <code>MyDerivedHouse<\/code>.<\/li><\/ul>\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>In summary, while both internal and private specifiers restrict access to variables and functions in Solidity, they differ in their scope of accessibility. Private restricts access solely to the defining contract, akin to a locked room, while internal extends that access to derived contracts, resembling a family room open to all family members. Understanding these nuances is crucial for designing secure and efficient smart contracts.<\/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":7327,"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":[46,29],"class_list":["post-7326","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-answers","tag-solidity","tag-web3"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7326","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=7326"}],"version-history":[{"count":3,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7326\/revisions"}],"predecessor-version":[{"id":7339,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/7326\/revisions\/7339"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/7327"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=7326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=7326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=7326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}