11

In Solidity we have four types of access. Two of them are private and internal. What is the difference if both of them can be used inside smart contract and both of them are not visible after deploying?

Adam Kozlowski
  • 5,606
  • 2
  • 32
  • 51
  • By `not visible after deploying` you mean they are not visible (and cannot be used) by a user or another smart contract. – user2340939 May 03 '23 at 13:37

3 Answers3

23

Access types:

public - can be used when contract was deployed, can be used in inherited contract

external can be used when contract was deployed , can NOT be used in inherited contract

internal - can NOT be used when contract was deployed , can be used in inherited contract

private - can NOT be used when contract was deployed, can NOT be used in inherited contract

Adam Kozlowski
  • 5,606
  • 2
  • 32
  • 51
17

internal properties can be accessed from child contracts (but not from external contracts).

private properties can't be accessed even from child contracts.

pragma solidity ^0.8;

contract Parent {
    bool internal internalProperty;
    bool private privateProperty;
}

contract Child is Parent {
    function foo() external {
        // ok
        internalProperty = true;
        
        // error, not visible
        privateProperty = true;
    }
}

You can find more info in the docs section Visibility and Getters.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
1
  • public: anyone can access the function
  • private: only this smart contract can call this function
  • internal: only this smart contract and smart contracts that inherit from it can call this function
  • external: anyone can access this function unless this smart contract

Note that external uses less gas than public so if the function is not used by your contract, prefer external over public.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
0xAnthony
  • 36
  • 1