What is difference between internal and private in Solidity?

Viewed 4348

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?

2 Answers

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

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.

Related