1. When should you put the virtual keyword on a function?
You should put the virtual keyword when you want to make it possible to override this function from a contract that inherits from it.
2. When should you put the keyword override on a function?
When you want the function to be used instead of the function from the base contract.
3. Why would a function have both virtual and override keywords on it?
A function may need to override a function from an inherited contract but also need the ability to be overwritten. This is necessary for base contracts that inherit from and interface.
-
When you allow for that function to be overridden by an inherited contract.
-
When you want that function to override a function in a parent contract.
-
A function will have both the virtual and override markers if it is overriding an existing function (override) in a parent contract and can itself be overridden (virtual) by another contract that inherits your current contract.
-
When should you put the virtual keyword on a function?
If you want a function that allows an inheriting contract to override its behavior. -
When should you put the keyword override on a function?
When the function overrides the base function. -
Why would a function have both virtual and override keywords on it?
Because you may want to override the function and make it available for other inheriting contracts to override it again.
-
When should you put the virtual keyword on a function?
The keyword virtual should be used when the functions inherited behavior can be overwritten. -
When should you put the keyword override on a function?
The override keyword should be used in a function that will replace the behavior of an inherited function. -
Why would a function have both virtual and override keywords on it?
A function would have both the virtual and the override keyword when it inherits a behavior that can also be replaced when inherited by another function.
1. When should you put the virtual keyword on a function?
A function that allows an inheriting contract to override its behaviour 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?
The function that overrides that base function should be marked as override but if it also can be overridden that need to have both virtual and override.
1. You mark a function as "virtual" to allow an inheriting contact the ability override it's behaviour.
2. A function that overrides a base function will use the keyword "override".
3. Both are used in a function definition to allow an inheriting contact the ability to override it using the "virtual" keyword and use the "override" keyword so it can inherit from a contact it calls.
-
When should you put the virtual keyword on a function?
When it allows an inheriting function to override it’s behavior. -
When should you put the keyword override on a function?
When the function overrides base function. -
Why would a function have both virtual and override keywords on it?
When a function overrides a base function and at the same time allows itself to be overridden.
-
When should you put the virtual keyword on a function?
When we create a base contract with a function that is meant to be overwriten by a child contract. -
When should you put the keyword override on a function?
If we override a base contract function in a child contract. By using only override the function is no longer virtual for future inheritance. -
Why would a function have both virtual and override keywords on it?
If we override a base contract function in a child contract and want future inheritance to still be able to override our current override
-
When you want to give a function the oppurtunity to be overridden by the same function from an inheriting contract.
-
When you wish to override a function that is the same as yours, but inherited from another contract.
-
When you want to be able to override a function from an inherited contract, but at the same time allow inheriting contracts to override your function.
- When I want an inheriting contract to override that function.
- When I want to override a virtual function from an inherited contract.
- When it both overrides a function from an inherited contract and allows for inheriting contracts to override it.
- On a parent function if you want a child contract to be able to override it.
- On a child function that is overriding a parent function.
- Because it is overriding its own parent function as well as allowing a child function to override it.
-
You should put the virtual keyword on a function when it allows an inheriting contract to override its behavior.
-
You should put the override keyword on a function when it overrides a base function.
-
A function that has both virtual and override keywords allows overriding of that function as well as, it also overrides the base function
-
To mark the the functions in the base contract that could be overridden in derived contracts.
-
To mark the function(s) in the derived contract that override (replace) the corresponding function(s) in the base contract.
-
Functions in contract A marked with virtual and override replace functionality in the base contract B while allowing a function in derived contract C to potentially replace these functions.
1 - A function that allows an inheriting contract to override it’s behaviour will be marked as virtual. Also, all of the functions in interface contracts are automatically considered virtual.
2- The function that overrides that base function should be marked as override . If your contract is inheriting the same function from multiple base contracts (that are unrelated), you must explicitly state which contracts: override(Base1, Base2)
3- It can be inherited from an another contract, and at the same time it can allows an inheriting contract to override it. Also it can be in the interface contract as well.
-
When should you put the virtual keyword on a function?
virtual specifies a function that can be overriden by an inherting contract. I’d use it if I want child contracts to substitute this function with their own. -
When should you put the keyword override on a function?
Override specifies that this function overrides the (virtual) function in the parent contract.
When there are multiple parents override needs to be followed by (contract A, contract B,… ) to specifiy which parent functions to override. -
Why would a function have both virtual and override keywords on it?
When implementing an interface and allowing child contracts to override the implementation.
-
When should you put the virtual keyword on a function? A function that allows an inheriting contract to override its behavior will be marked as virtual .
-
When should you put the keyword override on a function? The function that overrides that base function should be marked as override .
-
Why would a function have both virtual and override keywords on it? it has both so that you can inherit the function from a different contract and override it
- The virtual keyword allows an inheriting contract function to be overridden.
- The function that overrides an inherited function (marked as virtual) should be marked as an override.
3.When a function overrides an inherited function and sets itself to be overridden it should be marked virtual and override.
Openzeppelin Reading - Assignment Answers
-
When should you put the virtual keyword on a function?
When we want to allow an inheriting contract’s function to override a base contract’s behavior we
label that function as virtual. -
When should you put the keyword override on a function?
When changing the behavior of a base contracts function we label it with the keyword
override from the inheriting contract. -
Why would a function have both virtual and override keywords on it?
So that if your contract is inheriting the same functions from multiple base contracts we can
explicitly state which functions to override
1. When should you put the virtual keyword on a function?
When a function is being overridden by a function of the inheriting contract.
2. When should you put the keyword override on a function?
When a function overrides the function of the base contract.
3. Why would a function have both virtual and override keywords on it?
When a function overrides the function of its base contract it must have the overrides keyword. And the function allows to be overridden by a deriving contract it must have the virtual keyword.
-
When should you put the virtual keyword on a function?
When we want contracts that inherit ours to be able to override the function. -
When should you put the keyword override on a function?
When we want to override the function from a base contract. -
Why would a function have both virtual and override keywords on it?
When we override a function from a base contract, and want to allow inheriting contracts to override our function as well.