Assignment - Openzeppelin Reading

  1. When you want to allow a child contract’s function to override the preexisting contract’s function

  2. When you want to change details of the function from its parent contract

  3. A function would have both keywords in it if it’s changing its parent contract but also allows an inherited contract to change the function.

1 Like
  1. When should you put the virtual keyword on a function?
    When you want an inheriting contract to be able to override the function.

  2. When should you put the keyword override on a function?
    When a contract is overriding a function marked as virtual in a contract it is inheriting from.

  3. Why would a function have both virtual and override keywords on it?
    When the contract inherits a virtual function it wants to override but can also be inherited by contracts that want to override it.

1 Like
  1. When should you put the virtual keyword on a function?
    when you want an inherited contract override the function in other words make your own function in your contract
  2. When should you put the keyword override on a function?
    when you want to make your own version of the function
  3. Why would a function have both virtual and override keywords on it?
    This contract can override the function and any child contract can override it.
1 Like
  1. When you gives the option your function to be overridden

  2. If you use the same function name of an inherit contract

  3. When you want for your function 1. and 2.

1 Like
  1. We use the virtual keyword whenever we want to declare that a function needs to be overridden from a child contract.
  2. We use override when we actually implement the virtual function.
  3. override will be used to declare that the implementation will follow in the function body, and virtual will be used to allow following child contracts to re-implement/override the function.
1 Like

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

Both would be used when the function is overriding a parent function but also allowing children to override it.

1 Like
  1. When should you put the virtual keyword on a function?
    A function that allows an inheriting contract to override its behavior will be marked at virtual .
  2. When should you put the keyword override on a function?
    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?
    Virtual if we allow our function to be overridden by an inheriting contract with a function with the same name as ours, and we use the marker Override if our based contract/function is inheriting from a contract containing a function with the same name as our base function as it is the case of Interface (IERC20) contracts that by default all their functions are virtual.
1 Like

Q1. When should you put the virtual keyword on a function?
If we want the function in base contract to be overridden by a function in inherited contract.

Q2. When should you put the keyword override on a function?
If we want the function in inherited contract to override a function in base contract.

Q3. Why would a function have both virtual and override keywords on it?
A function in a contract will have both virtual and override keywords if it allows override in inherited contract and at the same time overrides a function in interface/parent contract.

1 Like
  1. A function that allows an inheriting contract to override its behavior has the virtual keyword
  2. A function that overrides a function on a base contract is marked as override.
  3. A function that inherits another function but can also be overridden by another function.
1 Like

1.When you want to allow the inheriting contract to override it’s behaviour.
2.So the function can override the base function from the inherited contract.
3. If the function already replaces the parent function but it also allows itself to be overwritten by an inherited contract function.

1 Like

1. When should you put the virtual keyword on a function?
You should put virtual on your function if you want that function to be overridden by your child contracts

2. When should you put the keyword override on a function?
Override function is used to override a function inherited from a parent contract that has virtual in the function header

3. Why would a function have both virtual and override keywords on it?
A function has both virtual and override if it inherits a contract from a base contract and overrides it, and that function can also be inherited and overridden by a child contract from it.

2 Likes
  1. When you want your inherited contract to override its behavior.
  2. you write the override keyword when your contract inherit another contracts interface which have the same named function
  3. because if you write the override keyword where your contract inherit another contracts interface which have the same named function,All functions in interface contracts are automatically considered virtual.
1 Like
  1. When should you put the virtual keyword on a function?
    put the virtual keyword in a function when you want to be able to override that function in a derived contract. functions in instances are now automatically virtual even if the virtual keyword is not in the function header.

  2. When should you put the keyword override on a function?
    use the override keyword in a function header when you want to override the functionality of an inherited function. In most cases the function being overridden must have the virtual keyword in its function header.

  3. Why would a function have both virtual and override keywords on it?
    You would use both keywords in a function when you want a function in a derived contract to be able to override it. At the same time the function would also be able to override a function from an inherited contract.

1 Like
  1. When should you put the virtual keyword on a function?
    The virtual keword should be used when a function is inherited from a base contract such as an interfaced function derived from another contract and when you want to override the base function.

  2. When should you put the keyword override on a function.
    The override keyword should be used when the same function is being inherited from multiple base contracts so one can be explicit over which base contract, overrides the other.

  3. Why would a function have both virtual and override keywords on it?
    virtual functions are functions which are allowed to be modified but then need to be explicitly identified; as to which base will override the other.

1 Like
  • When should you put the virtual keyword on a function?
    The function that allows an inheriting contract to override its behavior.
  • When should you put the keyword override on a function?
    The function that overrides that base function.
  • Why would a function have both virtual and override keywords on it?
    In case inheriting from some particular base contract and then override a function in that base contract.
1 Like
  1. Virtual keyword should be put on the function that allows an inheriting contract from the current contract to override its behavior.

  2. Override keyword should be put on the function that overrides its base function.

  3. Both keywords should be put on the function that allows an inheriting contract to override its behavior, and if the function inherits different contract that overrides its base function.

1 Like
  1. When should you put the virtual keyword on a function?
  • When you wish to allow inherited contracts to override said function.
  1. When should you put the keyword override on a function?
  • When writing a function that overrides an inherited virtual function
  1. Why would a function have both virtual and override keywords on it?
  • If the function is overriding a parent contract (override) & simultaneously allowing for a child contract to override said function in turn (virtual)
2 Likes

Hi All,

  1. When should you put the virtual keyword on a function?
  2. When should you put the keyword override on a function?
  3. Why would a function have both virtual and override keywords on it?

Answers:

1- When you would like to override a function by an inherited contract.
2- When you would like to override the base function with a function.
3- Every interface function has the virtual keyword on it. If you have a function which inheriting from another contract and you have the same function name(or inheriting the same function from multiple contracts), you woud need to add the override keyword due to you will get the following error: TypeError: Overriding function is missing “override” specifier

1 Like

1. When should you put the virtual keyword on a function?
When we want to override a function by an inherited contract.
2. When should you put the keyword override on a function?
When a function override the same fucntion from base contract.
3. Why would a function have both virtual and override keywords on it?
To allow a function being override in a parent contract and also able to being override from an inherit contract.

1 Like

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

When you want a function part of a base contract to be overridden by another contract inheriting it.

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

When you want to override a function inherited from a base contract.

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

Because that function belongs as part of an interface in which case all functions coming from interfaces are automatically considered virtual.