Are private variables in solidity inherited ?
Please I need a bit of clarification here from most text I have read about inheritance in solidity they state clearly that private variables of parent contracts are not inherited by child contracts but I think this is false. I think the private variables are inherited by child contracts but are not accessible to the child contract directly. Lets consider the contracts below:
contract A {
uint256 private num;
function setNum(uint256 _num) public {
num = _num;
}
function getNum() public view returns(uint256) {
return num;
}
}
contract B is A {
function getSlot(uint256 _slotNum) public view returns(uint256 _num) {
bytes32 slot;
assembly {
slot := sload(_slotNum)
}
_num = abi.decode(abi.encode(slot), (uint256));
}
}
from the contracts above contract B inherits the setNum
and getNum
functions from the contract A and does not inherit the private state variable num right ? But if I call B.setNum(34)
then B.getNum()
will return 34 (this means the contract B has a uint variable that is being set). Also if I call B.getSlot(0)
it returns 34 which confirms that contract B actually inherits num variable from contract A.