Assignment - Openzeppelin Reading

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?

  1. If you intend a function to be (optionally) overwritten
  2. If you intend to overwrite a function of an inherited contract
  3. When a function overwrites another and allows to be overwritten itself, e.g. when implementing an interface
1 Like

1. When should you put the virtual keyword on a function?
To let the inheriting contract override this function.

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

3. Why would a function have both virtual and override keywords on it?
When a function overrides a base contract function and lets the child contract do likewise.

1 Like

1 - when you want an inheriting contract to be able to override the function

2 - put override on a function if you want to replace the inherited function with your own code, this can be done if the inherited function is virtual

3 - virtual indicates that the function can be overridden by a child and override indicates that the function is overriding the parent function

1 Like
  1. When should you put the virtual keyword on a function?
    if the function of a base contract can be overriden by a function from a contract inheriting from the base contract.

  2. When should you put the keyword override on a function?
    if the function in the inheriting contract can replace the function in the contract it inherits from

  3. Why would a function have both virtual and override keywords on it?
    The inheriting contract might be inherited from as well. As such it works in 2 directions: for one it needs to be capable to override its parent contract, and for the other, it needs to be enabled to be overridden itself by a child contract inheriting from it.

1 Like
  • When should you put the virtual keyword on a function? When you want to allow subclasses of your class (contract) to redefine this function
  • When should you put the keyword override on a function? When you are redefining a function that is is marked “virtual” in the base class.
  • Why would a function have both virtual and override keywords on it? You can redefine a function in your contract and allow contracts that inherit from your contract to further redefine the function.
1 Like
  1. When should you put the virtual keyword on a function?
    When you want a function to be overwritten by another contract

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

  3. Why would a function have both virtual and override keywords on it?
    To have another contract to overwrite the function by another function

3 Likes
  1. The virtual keyword allows a function’s behaviour to be overriden in the inheriting contract.

  2. The override keyword is used in the function’s header to override behaviour of the base function.

  3. It is so that the function can be overriden by any other inheriting contracts. For example, in the ERC20.sol, the transfer function’s header contains the virtual and override keyword. If this contract is inheritied by another contract, the transfer function can be overriden using the override keyword.

2 Likes

:one: You put the virtual keyword in a function when you are happy for an inheriting contract to override the functionality of a particular function, assuming a child contract will use the same function as the parent.

If you explicitly don’t want a child contract to potentially overwrite the functionality of a function, then you should not include the virtual word on a function.

:two: You should put override on a function when you want this particular function to override the functionality of another virtual function in another child contract. In this case, the virtual function will inherit from the override function.

If the inheriting function inherits an override function from the parent contract, but it does not include the virtual keyword, it will not inherit functionality from the parent contract.

:three: A function can have both the virtual and override keywords on it when it requires that it will inherit some function from a parent contract, and also provide inheritance to a different child contract.

A common example of this is if you were to inherit from the OpenZeppelin ERC20.sol file and decide to make your own transfer function. You could write a transfer function which is virtual by inheriting from the ERC20.sol file, and also and override function if it expects functions to not be modified if they inherit from your contract.

1 Like

Solution for ‘Virtual and override keywords’ reading assignment

1. When should you put the virtual keyword on a function?
A function should be tagged with the ‘virtual’ keyword when any inheriting contracts should be allowed to override it.

2. When should you put the keyword override on a function?
The ‘override’ keyword should be added to any inherited ‘virtual’ function that you override in your contract.

3. Why would a function have both virtual and override keywords on it?
A function that has both the ‘virtual’ and ‘override’ keywords means that it is:

  • overriding a virtual function in a base contract or interface
  • itself a base contract that can be inherited and overriden
2 Likes

Here are my answers:
1. When should you put the virtual keyword on a function?
When we want to allow the function to be overridden.

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

3. Why would a function have both virtual and override keywords on it?
In the case that the function overrides and inherited function and we also want to allow other developers to override our function.

1 Like

.1. When should you put the virtual keyword on a function?
=> If you want that function to be overridden by an inheriting contract.

.2. When should you put the keyword override on a function?
=> To replace the same named function in an inherited contract(s).

.3. Why would a function have both virtual and override keywords on it?
=> If itself overrides that functiojn in a base contract and might be overriden itself.

1 Like
  1. When should you put the virtual keyword on a function?
    When it is okay for the function to be overwritten by another contract

  2. When should you put the keyword override on a function?
    When the function is intended to overwrite a function in another contract

  3. Why would a function have both virtual and override keywords on it?
    When it is both intended to overwrite a base contract function but you would want to include the functionality of allowing it to be overwritten itself.

1 Like
  1. The virtual keyword on a function should be used when you want the function to have the ability to be overridden.

  2. The override keyword on a function should be used when you are overriding a virtual function from a parent contract.

  3. A function would have both if it is overriding a parent contract and if it also wants child contracts to override its functions also.

1 Like
  1. When should you put the virtual keyword on a function?
    Ans1. When we want to override the function in future to add additional logic or let people add additional logic to it.

  2. When should you put the keyword override on a function?
    Ans2. When we want to override the function i.e. put additional logic into it.

  3. Why would a function have both virtual and override keywords on it?
    Ans3. When we are importing the function from somewhere but still want it to be editable in terms of adding logic into the function when we or someone else imports the contract in future.

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

The ‘virtual’ keyowrd marks a base function that can be overriden.

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

We put ‘override’ in the header of the new function overriding the base function.

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

We woud put override on a function if we are overriding a base function now, but also want to allow the overriding function to be overriden in the future by another developer.

1 Like
  1. A function should include ‘virtual’ as a keyword when it is meant to be (or can be) overridden by a contract that inherits from the the original contract.

  2. A function should include the keyword ‘override’ when it overrides a function that already exists in a parent contract.

  3. A function would have both ‘virtual’ and ‘override’ as keywords if it is in a contract that is both a parent and a child contract. The function can both override functions in the parent contract and be overridden by functions in the child contract.

1 Like
  1. A function that allows an inheriting contract to override its behavior will be marked at
    virtual .

  2. if you are using a function from an. inherited contract and you want to override its functionality you add override to the function header.

  3. in this case it means that the function is already overriding function from another inherited base contract.

1 Like
  1. When should you put the virtual keyword on a function?
    When you want your function to be overriden by a daughter contract

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

  3. Why would a function have both virtual and override keywords on it?
    If a contract has both parent and daughter contracts. Virtual will allow daughter contracts to override the function while override will overwrite the inherited function from parent.

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

When you expect that function to be overridden by an inheriting contract’s function.

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

When the function overrides a base contract’s function.

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

If the function inherits from an interface contract and it’s expected to be overridden by an inheriting contract’s function

1 Like
  1. When should you put the virtual keyword on a function?
    When you want to overide this function

  2. When should you put the keyword override on a function?
    When the function is the one that´s overriding

  3. Why would a function have both virtual and override keywords on it?
    When it´s replacing a parent function but also can be inherited and replaced in a child contract

2 Likes