Assignment - Openzeppelin Reading

When should you put the virtual keyword on a function?

A function that allows an inheriting contract to override its behavior should be marked as ‘virtual’

  • Functions without implementation should be marked ‘virtual’ outside of interfaces

  • All functions in interface contracts are automatically considered virtual (no ‘virtual’ declaration is required)

    • e.g., see @openzeppelin IERC20.sol which is the Interface of the ERC20 standard as defined in the EIP
  • Functions with the ‘private’ visibility cannot be ‘virtual’

When should you put the keyword override on a function?

A function that overrides a base function should be marked as ‘override’

  • In the case of multiple inheritance (base contracts not related), all direct base contracts should be specified explicitly

  • In the case of multiple inheritance (on some path through the inheritance graph), the most derived base contracts that define the same function must be specified explicitly after the override keyword.

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

A function that is marked as both ‘virtual’ and ‘override’, means the function overrides a base function and also allows an inheriting contract to override its behavior

1 Like

answer: Virtual keyword allows the function to be overridden by inheritor contract.

answer: Override keyword is used when the function from inherited contract needs some change.

answer: A function would have both virtual and override keywords as the function needs to override the inherited contract’s function and also be able to be overridden by other contract which inherits the given functions’ contract.

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

If you want the function to have the possibility to be overwritten in an inheriting contract you declare a function as virtual.

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

Override is used in an inheriting contract to change a virtual function of the parent contract.

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

It can be used to override the function of older contracts but give the possibility for fututre inheriting contracts to also override the same function.

1 Like

1.When should you put the virtual keyword on a function?
If you want the function to have the possibility to be overwritten in an inheriting contract you declare a function as virtual.

2.When should you put the keyword override on a function?
The reason the OpenZeppelin transfer function includes the override keyword is because it’s inheriting the IERC20 interface which also has a transfer function.

3.Why would a function have both virtual and override keywords on it?
A function that allows an inheriting contract to override its behavior will be marked at virtual.

  1. When the function needs to be overridden by inherited contract
  2. When you want the function to override function in base contract
  3. A function can have both the keywords so that a function can override in the base contract and also able to being override from an inherit contract(if required)

Hi All,

Here are my answers to the questions asked:

  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. If that function can be overwritten by a contract that inherits from it.
    Function A in Contract Parent = virtual
    Function A in Contract Child = override => If extra code is added in this contract, it will override the function in Contract Parent

  2. If that function is overriding code in the Parent Contract. Override has the advantage comparing it to virtual

  3. That’s because that particular function inherits from another contract. Still that function can be overwritten if there’s another contract.
    Contract A has function Z => Contract B has function Z
    Contract A = virtual
    Contract B = override (overwriting code in Contract A)

Contract B has function Z => Contract C has function Z
Contract B = virtual
Contract C = override (overwriting code in Contract B)

Contract B = virtual (because Contract C inherits from Contract B)
This means that Contract B is both virtual and override, because it’s inheriting from Contract A, but Contract C inherits from Contract B. That’s why it’s both virtual and override.

1 Like
  1. When should you put the virtual keyword on a function? If you want to allow derived classes to override that function

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

  3. Why would a function have both virtual and override keywords on it? If your derived contract were itself usable as a base contract in a “chain” of derivations.

1 Like
  1. When should you put the virtual keyword on a function?
    When we need the function to to be overridden by an inheriting contract.
    "Base functions can be overridden by inheriting contracts to change their behavior if they are marked as virtual".
    “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?
    When we want that function to override the behavior of another function in the base contract.
    “The overriding function must then use the override keyword in the function header”.
    “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?
    To indicate that it both overrides a function in the base contract and can be overridden in an inheriting contract.

1 Like

1. When should you put the virtual keyword on a function?
When we want child contracts to be able to override that function

2. When should you put the keyword override on a function?
We must declare the override when overriding a function marked as virtual (or an interface function, which is implicitly virtual)

3. Why would a function have both virtual and override keywords on it?
Because the two things are not exclusive. A function can override a parent contract virtual one (so it has to be declared with override) and let further children contracts to override it (so it can be declared with virtual)

1 Like
  1. The base contract function that is meant to be inherited should be marked virtual, to tell developers it is meant to be overridden.

  2. The contract function that is overriding the function in the inherited contract should be marked override, in order to let us know that we are overriding a function in the base contract.

  3. Since Solidity is a language that supports multiple inheritance, we can mark a function as overriding a contract that we are inheriting from, while also marking a contract virtual to let us know that we can override it again in another child contract.

Read the attached PDF about the override and virtual keywords in solidity. Then answer the following questions in the forum.

  1. When should you put the virtual keyword on a function?
    You should put the virtual keyword on a function that allows an inheriting contract to override its
    behavior.

  2. When should you put the keyword override on a function?
    You should put the override keyword when a function is execution needs to update/override
    another function with the virtual keyword.

  3. Why would a function have both virtual and override keywords on it?
    You would use both virtual and override if the function will be inherited from another contract and
    if this function needs its value to be updated for some use case.

1 Like
  1. When should you put the virtual keyword on a function? You declare a virtual function when your function needs to be overwritten.

  2. When should you put the keyword override on a function?
    The overriding function allow for more explicit behaviour when overriding functions.

  3. Why would a function have both virtual and override keywords on it? The purpose of these keywords is to be more explicit when overriding a function. Base functions can be overridden by inheriting contracts to change their behaviour.

1 Like

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

  • When you want a function to be overridden by an inherited contract.

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

  • When you want to override a parent contract function.

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

  • I believe in the case that you want the function to override the parent contract function and to be able to be overridden by another function.
1 Like
  1. When should you put the virtual keyword on a function?
    When you want a function to be overridden by an inherited contract

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

  3. Why would a function have both virtual and override keywords on it?
    When you want that function to be both overridden by an inherited contract and you want it to override the parent function.

1 Like
  1. When should you enter the virtual keyword on a function?
    -> if I want to change the function in future child contracts.

  2. When should you put the override keyword on a function?
    -> when in a child contract, I want to modify a function
    succession from the parent company contract

  3. Why should a function have both virtual keywords and overrides on it?
    —> because he inherited a function which he later modified. and which can be modified in child contracts

1 Like

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

Whenever you want to allow child contracts to override the function

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

When you want to override an existing function in the parent contract.

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

When you want to override the parent function and allow the child to override this one.

1 Like
  1. The virtual keyword should be used on functions when inheriting contracts should be allowed to override them.
  2. The override keyword is used when a function from the parent contract is being re-declared in a child contract (the keyword is placed in the header of the child contract’s function).
  3. If a contract’s function overrides its parent’s function and needs to allow its children the opportunity to override the function again, then both the virtual and override functions are required.
1 Like

1 the virtual keyword should be marked to a function that would be overridden if inherited by another
contract.

2 The override keyword should be added to a function that overrides the base contract(the contract
inherited).

3 A function with the virtual and override keyword is one that had overridden a parent contract, and
can still be overridden by a contract inheriting it.

1 Like
  1. When should you put the virtual keyword on a function?
    If we want a function to be overridden by its child class then we use virtual keyword on that function. In other words by using virtual keyword in a function , we are giving permission to the child class to override the given function.

  2. When should you put the keyword override on a function?
    When we define new functionality to the function of parent class we have to use override keyword on that function. override keyword denotes that we have overridden the function of parent class.

  3. Why would a function have both virtual and override keywords on it?
    A function which have both keywords virtual and override means that the said function has been defined differently and has been overridden than the function in parent class. and the class which inherits this class can also override this function. for e.g
    class A {
    function foo() virtual{
    }
    }

class B is A {
function foo() virtual override{
}
class C is B {
function foo() override {
}
}

class B has overridden function foo defined in class A , now if class B wants that class C inheriting from class B can also override function foo then it has to use both keywords virtual and override in it.

1 Like
  1. When the function is to be overridden (or expected to be)

  2. When you’ve overridden a function from a base contract (the base’s virtual function).

  3. When expecting layered inheritance in a contract already inheriting. A case could be the ERC20 contracts we have been making. These in turn have been used as bases for other contracts.