Inherited OZ functions

Hi everyone,

First question : I am importing the ERC20.sol OZ smart contract and I noticed on this forum that the common practice regarding the _beforeTokenTransfer function is:

  • Taking ERC20.sol as a parent contract from which my own contract inherit.
  • Overriding this _beforeTokenTransfer function from my child contract if I want to add some action before each token _transfer.

I understand therefore that the _transfer function in the ERC20 parent contract, once called, is also calling the _beforeTokenTransfer function in the child contract. Is that not an issue, meaning a parent contract function calling a child contract function ? Overriding means totally replacing a parent function from the child contract, not taking into account the inheritance situation ?

Second question : if I put a _beforeTokenTransfer function in my child contract overriding the ERC20 parent function, can I also call from the child function another function in my child contract ? Meaning :

  • I implement the overriding _beforeTokenTransfer function in my child contract
  • This overriding function, once called from the _transfer function in the ERC20 parent contract, can call during its execution another function in the child contract.

Thanks in advance.

Hi, welcome! :wave:

For example:

Contract A {}         // <<-- Parent Contract
Contract B is A {}    // <<-- Child Contract

I think you will deploy the contract B, so actually, you will call all functions from contract B.

Yes, I think you can, and you can have a try.
For example:

Contract A {
    function _beforeTokenTransfer() internal virtual {}
Contract B is A {
    function do_something() internal {
    function _beforeTokenTransfer() internal override {

Thanks, I will try it when back home.

I am just surprised for the first answer, it means in your example : if a function in contract A calls a function in contract B (with B inheriting from A and being the one deployed), it actually works. I thought parent functions could not call functions in the child contracts.