How do Function Visibility Modifiers and Inheritance Work Together?

Inheritance is the property in which the child contract uses the features of the parent contract. In Solidity, the keyword ‘is’ is used to provide inheritance. The function visibility is set in the parent contract according to which the child contract can use its parent’s function. The function visibility of the function provides the accessibility of the function to the child contract. The visibility defined in the parent contract is used to identify whether the child contract can use the function or not or if it is private to the parent contract only.

The four function visibility modifiers are: public, private, internal, and external. The accessibility of the function by child contract is defined by the following table:

Parent Function Visibility Specifier Child Function Accessibility
public Can access the function
private Cannot access the function
internal Can access the function
external Cannot access the function

Below is the Solidity program to implement the function visibility modifiers:

Solidity




// Solidity program to implement 
// the function visibility modifiers
  
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
  
contract A {
  public uint p = 10;
  function Functionint ()internal pure returns (uint p) 
  {
    //This function is internal and can be used 
    // by same contract or child contract.
    return p;
  }
}
  
Contract B is A{
  function funint() public returns (uint)
  {
    // Internal function of parent
    return Functionint();
  }
}
  
contract P{
  B b = new B();
  uint a;
  function f() public returns (uint)
  {
    a = b.funint();
    return a;
  }    
}


 

Function Visibility Specifier in Solidity

Function in Solidity is a set of code that performs a specific task. The function provides reusability of code in smart contracts. In Solidity, functions are defined using the function keyword. The syntax of the function definition in Solidity is:

function function_name (parameter list) visibilty_specifier modifier returns (return_type)
{
   Block of Code
}

Function visibility in Solidity defines the visibility of the function to other functions within the contract, or in other contracts. Function visibility helps to identify where in the contract or in other contracts, functions can be used. It is specified by the developer of the smart contract. The default variable visibility in Solidity is internal. The default function visibility in Solidity is public.  

Similar Reads

How do Function Visibility Modifiers and Inheritance Work Together?

Inheritance is the property in which the child contract uses the features of the parent contract. In Solidity, the keyword ‘is’ is used to provide inheritance. The function visibility is set in the parent contract according to which the child contract can use its parent’s function. The function visibility of the function provides the accessibility of the function to the child contract. The visibility defined in the parent contract is used to identify whether the child contract can use the function or not or if it is private to the parent contract only....

Function Visibility Modifiers

...

Function Visibility Modifier vs State Variable Visibility Modifier

The function visibility specifier helps in specifying the type of visibility of the function. It is like access specifiers in other OOP languages such as C++. The function visibility specifiers define the visibility of the function within the entire smart contract. There are four function visibility specifiers in Solidity: public, internal, external, and private....