I have come across a popular smart contract in the OpenZeppelin library that manages access control of a contract. This is the library: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol
Looking at the last two functions, transferOwnership
is marked public
and virtual
and calls _transferOwnership
to perform its functionality basically, which is marked as internal
and virtual
. What does this pattern achieve? Or why isn't simply like this:
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
Is is because transferOwnership
has a modifier which restricts access, so it won't make sense to make it internal?
Does it have to do something with security or reusability or the virtual modifier?