0
0
Blockchain / Solidityprogramming~5 mins

Visibility modifiers (public, private, internal, external) in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

Visibility modifiers control who can use or see parts of your code. They help keep your program safe and organized.

When you want a function to be used by anyone, like a public service.
When you want a function to be used only inside the same contract to keep it private.
When you want a function to be used inside the contract and contracts that inherit it.
When you want a function to be called only from outside the contract, not from inside.
Syntax
Blockchain / Solidity
function functionName() public { }
function functionName() private { }
function functionName() internal { }
function functionName() external { }

public: Anyone can call this function.

private: Only the current contract can call this function.

internal: Current contract and contracts that inherit it can call this function.

external: Only outside callers can call this function, not internal calls.

Examples
This function can be called by anyone, even outside the contract.
Blockchain / Solidity
function greet() public {
    // Anyone can call this
}
This function is hidden from other contracts and users.
Blockchain / Solidity
function secret() private {
    // Only this contract can call this
}
This function is for internal use and inheritance.
Blockchain / Solidity
function helper() internal {
    // This and child contracts can call this
}
This function cannot be called inside the contract itself.
Blockchain / Solidity
function externalCall() external {
    // Only external calls allowed
}
Sample Program

This example shows how different visibility modifiers control access to variables and functions. Private items are only for the contract itself. Internal items can be used by child contracts. Public items are open to everyone.

Blockchain / Solidity
pragma solidity ^0.8.0;

contract VisibilityExample {
    string private secretMessage = "Hello Private!";
    string internal internalMessage = "Hello Internal!";
    string public publicMessage = "Hello Public!";

    function getSecret() private view returns (string memory) {
        return secretMessage;
    }

    function getInternal() internal view returns (string memory) {
        return internalMessage;
    }

    function getPublic() public view returns (string memory) {
        return publicMessage;
    }

    function callInternal() public view returns (string memory) {
        return getInternal();
    }

    function tryGetSecret() public view returns (string memory) {
        // Cannot call getSecret() here because it is private
        // return getSecret(); // This would cause an error
        return "Cannot access private function from here";
    }
}

contract ChildContract is VisibilityExample {
    function childAccess() public view returns (string memory) {
        // Can access internal but not private
        return internalMessage;
    }
}
OutputSuccess
Important Notes

Private functions and variables are not visible outside the contract, even to child contracts.

Internal functions and variables can be accessed by the contract and any contract that inherits it.

External functions can only be called from outside the contract, not from inside.

Summary

Public means anyone can use it.

Private means only this contract can use it.

Internal means this contract and child contracts can use it.

External means only outside calls can use it.