Assignment - Openzeppelin Reading

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.

1. When should you put the virtual keyword on a function?
You should use it when you are allowing the function to be overridden by the inherited contract.

2. When should you put the keyword override on a function?
You should use it when you want the function of a contract to override a function of its base contract.

3. Why would a function have both virtual and override keywords on it?
Because it offers both to be overridden and also to override another function.

1 Like
  1. When should you put the virtual keyword on a function?
  • When you want to allow an inheriting function to override it.
  1. When should you put the keyword override on a function?
  • When the child function is overriding a virtual parent function.
  1. Why would a function have both virtual and override keywords on it?
  • The function is overriding its parent function and allowing child functions (that inherit it) to override it.
1 Like

When should you put the virtual keyword on a function?
If you write a function and you want to allow it to be overridden you must mark it as virtual.

When should you put the keyword override on a function?
If your contract inherits from another contract and you want to change the behavior of a certain function you must use the keyword override. Only works on a function that is virtual in the base contract.

Why would a function have both virtual and override keywords on it?
If a contract is both child and parent contract and a certain function overrides functionality of it’s parent contract but also allows the child contract to override.

1 Like
  1. You put the virtual keyword when pulling from an interface or other function overrides the inheriting contract’s behavior.
  2. When inputting a function from an inherited function (adding your own transfer function in child contract) you use an override.
  3. You would use both when you inherit from an interface that overrides a similar function in that contract.
2 Likes

1. When should you put the virtual keyword on a function?
I put the virtual keyword on a function if I want to give an inheriting contract the ability to override my function (the base function).
2. When should you put the keyword override on a function?
When I want my function to override an inherited function (from the base contract). The function must be virtual in the base contract.
3. Why would a function have both virtual and override keywords on it?
Because it is both overriding a base function, and also gives the ability for an inheriting contract to override it.

2 Likes
  1. When should you put the virtual keyword on a function?
    When we create a base function that allows an inheriting contract to override its behavior.

  2. When should you put the keyword override on a function?
    When we create any function that overrides the base function

  3. Why would a function have both virtual and override keywords on it?
    This means if we inherit any base function such as ERC20.sol contract into our project, we can write our own transfer function and mark it as override since it’s overriding the base ERC20.sol transfer function and also mark is virtual to allow any inheriting contract function to overwrite it.

2 Likes
  1. When should you put the virtual keyword on a function?
  • A function that allows an inheriting contract to override its behaviour.
  1. When should you put the keyword override on a function?
  • The function that overrides that base function should be marked as override
  1. Why would a function have both virtual and override keywords on it?
  • When it is overriding another function from the base contract but also allows it to be overridden by another function from a child contract.
1 Like

1. When should you put the virtual keyword on a function?
With the virtual syntax you specify that this function can be overwritten when it is being inherited somewhere else.

2. When should you put the keyword override on a function?
you should use override when you want to change the functionality from a function that is already used in the inherited contract. And when you inherit from multiple contracts you have to specify which base you wanna override.

3. Why would a function have both virtual and override keywords on it?
You want to do this when you override a function and if you want the function to be overwritten.

1 Like

1. When should you put the virtual keyword on a function?
When you want to allow inheriting contract to override its behavior.

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

3. Why would a function have both virtual and override keywords on it?
The function is inherited from a parent contract and its behavior is overridden ( override ). Also the function behavior can also be overridden in its inherited contracts ( virtual ).

1 Like