Assignment - Openzeppelin Reading

1- A function should be declared as virtual if we want to allow other contracts that inherit from ours to override that particular functions implementation.

2- A function should have the keyword override if the intent is to override the implementation of an inherited function with the same signature.

3- A function can have both override and virtual keywords in case it is overriding an inherited implementation but we still want to allow it to be overriden by contracts that inherit from ours.

2 Likes
  1. a function must have the virtual keyword when it might be overridden or redefine in child contracts
  2. You put the override keyword on a function when it change the logic of the parent contract function marked as virtual
  3. a function have both virtual and override keywords on it because it is implementing an interface function which can also be redefine by child contract inheriting from the contract implementing the interface.
1 Like
  1. When should you put the virtual keyword on a function?
    If you are creating a contract that will be inherited and you want certain functions in the contract to be overridden or allow others to do so.

  2. When should you put the keyword override on a function?
    A function that allows an inheriting contract to override its behavior will be marked at virtual . The function that overrides that base function should be marked as override .

  3. Why would a function have both virtual and override keywords on it?
    If you have created a child contract that has a modified function of an existing inherited function that you need to change the behaviour for, then you will need to mark the new function with virtual and override.

2 Likes

When should you put the virtual keyword on a function?

  • a contract’s function is inherited from an interface

  • marks a base contract’s function that can be overridden

  • the function has no implementation.

When should you put the keyword override on a function?

When it should redefine a function in the base contract

Why would a function have both virtual and override keywords on it?

Because you can’t override a function in a derived contract that is not virtual in the base contract. Virtual explicitly states that a function can be overridden by the derived contract.

2 Likes
  1. When should you put the virtual keyword on a function?
    You should use the virtual keyword if other contracts might be inheriting your contract and you want the function to have the ability to be overridden.

  2. When should you put the keyword override on a function?
    If you are inheriting a contract and you want to override a function on the base contract, you should use override on your function.

  3. Why would a function have both virtual and override keywords on it?
    If your contract is inheriting from another contract but can also be inheritied, you may need/want to use both keywords in your function.

2 Likes
  1. “virtual” should be used in a function if you expect an inheriting contract to have the ability to override that function.

  2. “override” is just the opposite. It should be used when the function is going to override another function inside a parent contract.

  3. Both could be necessary depending on the situation. For example, ERC20.sol can be a parent or child contract, so the functions within the contract need to be flexible enough for both situations.

2 Likes

1-in the function we want to use for its children which we want to have different outcomes

2-in the function inheritating a past function so we can override its result for a different outcome

3- if you want to inheritate into a 2nd gen of functions, it needs to have virtual in the header.

2 Likes
  1. When should you put the virtual keyword on a function?

When a function allows a contract inherits and override it, needs te be marked as virtual.

  1. When should you put the keyword override on a function?

When a function which in the eventual contract needs to be overridden will be modified, needs to be marked as override.

  1. Why would a function have both virtual and override keywords on it?

When the function needed as overridden from an interface and will be needed overridden by the eventual contract.

2 Likes

Simple and informative explanation. Thanks.

2 Likes
  1. When should you put the virtual keyword on a function?

    • When you want the function, or allow the function to be overridden.
  2. When should you put the keyword override on a function?

    • When that function overrides another function that its respective contract inherits from.
  3. Why would a function have both virtual and override keywords on it?

    • If it can be overridden and overrides from another contract.
2 Likes

1. When should you put the virtual keyword on a function?
A function with the virtual keyword is used in a parent function so a child function can override it.

2. When should you put the keyword override on a function?
A function with the override keyword is used in a child fuction so it can overrride the parent function

3. Why would a function have both virtual and override keywords on it?
When the function want to override its parent function but also allow it’s child function to override it

2 Likes

1. When should you put the virtual keyword on a function?

When we want to allow internal contracts (= who inherit from our contract) to interact with our function, and if they want even override it.

2. When should you put the keyword override on a function?

When there is a virtual function with the same exact name in a parent contract and we want to edit it.
If there are multiple parent contracts that have that same function, we should specify each of the wants we want to override.

3. Why would a function have both virtual and override keywords on it?
It’s when its contract is in a sandwich in the sense that it both

  • imports the function that is virtual

  • but also it wants to keep it virtual for contracts inheriting from him so that they can optionally edit it their way.

In other words the contract acts as a Parent and other times as a Child contract.

Example the ERC20.sol contract

2 Likes
  1. When should you put the virtual keyword on a function?

When you want to allow a contract that inherits the parent contract and you want to allow that child to be able to override the inherited function.

  1. When should you put the keyword override on a function?

A child contract that is inheriting a parent contract and wants its function with the same name to override the parent’s function.

  1. Why would a function have both virtual and override keywords on it?

If the parent contract also inherits a function from an interface contract it overrides it as interface contract functions are automatically considered virtual, and wants to allow a child that inherits the same function to be able to override it.

2 Likes
  1. When should you put the virtual keyword on a function?

When function can be overridden by its inheriting function

  1. When should you put the keyword override on a function?

When the function can override its base function

  1. Why would a function have both virtual and override keywords on it?

When a function can be overridden by its inheriting function, and can override its base function

1 Like
  1. You should put the virtual keyword in the function of a contract that may be inherited by future contracts in which there may be a desire to override that function.

  2. You should put the override keyword in the function of a contract that inherited a contract with a virtual function counterpart that you desire to override in the inherited contract.

  3. A function can have the virtual and override functions in order to allow it to override an inherited virtual function, while also leaving it open to overrides from contracts that inherit it. This is often useful in contracts that will be shared with multiple developers.

1 Like
  1. When should you put the virtual keyword on a function?
    when its allowed to override this function in inheriting contracts

  2. When should you put the keyword override on a function?
    when you want to override the function that you inherited from a parent contract

  3. Why would a function have both virtual and override keywords on it?
    if you want to override the function from the parent contract but still want other inheriting contracts to
    override your function.

  1. When should you put the virtual keyword on a function?

=> When this function can be overridden.

  1. When should you put the keyword override on a function?

=> When you are overriding a function

  1. Why would a function have both virtual and override keywords on it?

=> When it is overriding another function but can still be overridden by subclasses.

1 Like
  1. When should you put the virtual keyword on a function?
    When you want to allow the function to be overwritten in a contract that inherits yours

  2. When should you put the keyword override on a function?
    When you want to override a function in a base class

  3. Why would a function have both virtual and override keywords on it?
    If it is inherited from another base contract and you still want the function to be overridable

1 Like

1. When should you put the virtual keyword on a function?

  • when you want a function to allow an inheriting contract to override its behavior.
  • Or if the function is in an interface contract.

2. When should you put the keyword override on a function?

  • when you want a function to override a base function.
  • Or if your contract is inheriting the same function from multiple base contracts that are unrelated,
    you must explicitly state which contracts, override(contract1, contract2).

3. Why would a function have both virtual and override keywords on it?

  • when a function is overriding a base function and you want to allow the overriding function to be overridden.
1 Like
  1. When should you put the virtual keyword on a function?

It is used to allow an inheriting contract to override the function.

  1. When should you put the keyword override on a function?

To override a function which has been inherited.

  1. Why would a function have both virtual and override keywords on it?

I believe that it is to override an inherited function, and to allow an inheriting contract’s function to also override.

1 Like