Assignment - Openzeppelin Reading

1. When should you put the virtual keyword on a function?
When we want to allow inheriting contract to override this function (which is marked as virtual).

2. When should you put the keyword override on a function?
When we want to override a function in base contract (from which is override function inhereted).

3. Why would a function have both virtual and override keywords on it?
When this function is inheriting from a base contract, so we want to override a base function (marked as override) and also when we want to allow inheriting function from this function to override this function (marked as virtual).

1 Like
  1. When you want your function to be override by a function in an inheriting contract.
  2. When you want to override a function in the base contract.
  3. Because it might override another function or it might be overriden.
1 Like
  1. The virtual keyword is used when you want the function to be modified by its children.

2.when you want to make that change you use over ride to start the process

  1. Functions can both overide and be over written in which case they must be declared
1 Like
  1. When should you put the virtual keyword on a function?
    On a contractā€™s function(s) that an inheriting (child) contract is allowed to override

  2. When should you put the keyword override on a function?
    On any functions that do override a (virtual) function in the parent contract.

  3. Why would a function have both virtual and override keywords on it?
    Where a contract that inherits from a parent (base) contract overrides a base function but that contractā€™s function may itself be overridden by another contract that inherits from it.

2 Likes

Assignment Openzeppelin reading (Answers)

  1. When function allows an inheriting contract to override its behavior, it should be marked with ā€œvirtualā€.
  2. When a function overrides a base function, it should be marked with ā€œoverrideā€.
  3. If your inheriting and you need to write your own transfer function you must add both or the compiler will prompt a TypeError.
1 Like
  1. When should you put the virtual keyword on a function?
    It is when a function is intended to be overriden by inheriting contracts.

  2. When you should put the keyword override on a function?
    It is when a function from inheriting contract is overriding a function coming from an
    inherited contract.

  3. Why would a function have both virtual and override keywords on it?
    It is because that certain function is overriding from an inherited contract and at the
    same time that function can also be override by its inheriting contracts.

1 Like

So the virtual and override keywords are new additions as of solidity v0.6.0, when they were introduced, they allow much more flexibility when inheriting from other smart contracts, especially when using multiple levels of inheritance when thing scan get tricky. So the purpose of these keywords is to be more explicit when overriding a function.

When should you put the virtual keyword on a function?
Base functions can be overridden by inheriting contratcs to change their behavouir if they are marked as virtual. So if you wish to give a future user of your contratc the ability to modifiy and change its implementation and functionailty then the virtual keyword should be included in your function definition

When should you put the keyword override on a function?
As i just said, base functions can be overridden by inheriting contratcs to change their behavouir if they are marked as virtual. The ovveriding function in this inherited contratc must be labelled with the override keyword in order to be able to actually do and override. This keyword allows developers to signal their intent for certain functions so that others have a better understanding of the purpose of the function. So not all inherited functions have to be override the option exists only for scenarios where the functionality is needed.

Why would a function have both virtual and override keywords on it?
If your like me then you noticed that many functions in the openzeppelin library contracts used both the override and virtual keywords in tandom in the same function. This is because a function may want to be virtual so that if some future user comes along and inherits from it then they may want to do their own ovveride. And if the function that they are overriding also is inheriting from some higher parent contratc (multiple levels of inheritance) then the function must also be labelled with the override keyword. We notice this a lot in the openzeppelin library since a many contratcs have multiple levels of inheritance. So to summarise itā€™s a good idea to use virtual and override if you are inheriting from a parent contract that you wish to override and that you also wish to give future developers the ability to override your new function at some point in the future.

For example in the screenshot below of my transfer function we can see that the function declaration is underlined so we have an error. In this transfer function i make a few modifications for the specific needs of my contract such as the require statements etc and then i call

ERC20.transfer(recipient, amount)

which is a call to the transfer function in the ERC20.sol contratc. However since i dont have the override keyword in my function statement solidity throws an error because were not specifying that we want to ovveride this function. Thus we must include it to get rid of the error.

override

(TYPO-- recipient == msg.sender should be !=) Note alswell that i have also included the virtual keyword argument in my function statement. But notice how if we remove the virtual keyword we dont get an error.

overide

Thats because we dont techically need the virtual keyword here. However in this case i wanted to give the option of future users of my contratct to have the ability to override my transfer function. (Not that anyone would ever be using my contratc i know lol) but i just wanted to include it anyway.

Perhaps though it could be good not to be over reliant on these keywords as they do seem to simplify the process of things a lot and saves us time having to actually sift through parent contracts to see whats actually going on in many functions. So perhaps that is something to consider??? just a thought

  1. Use the ā€œvirtualā€ keyword on a function when you want an inheriting contract to be able to override that (parentā€™s contract) function.
  2. Use ā€œoverrideā€ in a child contractā€™s function when you want that function to override the parentā€™s identically-named function.
  3. Using both keywords allows us to establish hierarchies of contracts, while still being able to design functions at the bottom levels of the inheritance hierarchy that can still override their parents/ā€˜grandparentsā€™ and so on.
1 Like
  1. When should you put the virtual keyword on a function? ā€” when function can be overridden in child contract

  2. When should you put the keyword override on a function? ā€” when function overrides virtual function from parent contract

  3. Why would a function have both virtual and override keywords on it? ā€” when function overrides a virtual function from a parent contract but can also be overridden by a child contract

1 Like

Here are my answers:

  1. Putting the virtual keyword on a function allows any contract that inherits mine the ability to change my function.

  2. Putting the keyword override on a function indicates that the contract is changing a function that it is inheriting.

  3. Putting both keywords indicates that I am changing a function that I am inheriting but also allows a contract that inherits mine in the future to change it too.

1 Like

Answers:

  1. You should put the virtual keyword in a function when you want the function to be overridden by an inherited contract.

  2. The keyword override should de used on a function when you want to override a function from a parent contract.

  3. When you use both virtual and override in a function is when its being overridden by a parent contract which is then also being overridden from an inherit contract.

1 Like
  1. When you want it to be potentially overriden.

2.When you are overriding a virtual function that is inherited.

3.This is when it is overriding a base function but also still allows it to be further overridden in a new implementation.

2 Likes
  1. So that you can re-implement it in a derived contract
  2. When you override a virtual method
  3. So that the function can still be virtual in the next derived contract.
2 Likes

1. When should you put the virtual keyword on a function?
When you want to allow another contract that is inheriting to override a functionality.
2. When should you put the keyword override on a function?
When you want to change the functionality of an inherited contract.
3. Why would a function have both virtual and override keywords on it?
When it is already altered(override), and is allowed to be changed(virtual), it will have both.

1 Like

Hello,

You add the virtual keyword to a function to indicate that it can be overriden by a function with the same signature in a derived contract.

A derived contract implementing a function with the same signature must add the override keyword to confirm it is overriding the virtual function of the base contract.

If this derived contract is itself inherited by other contracts it will mark the override function as virtual as well to allow the derived contracts to override the function.

Cheers,
Matt

When should you put the virtual keyword on a function?

If you want derived contract can override the base contract function

When should you put the keyword override on a function?

When function is reimplementing the base contract function

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

If the function has reimplemented the the base contract function & want to allow further reimplementation by the contract which it inherits it.

2 Likes

1: When inheriting another contract and you want to override the functionality of it.

2: Changing the function of an inherited contract or changing its functionality.

3: If the function is defined in an interface it must be marked with override. In order for it to be overridden, it needs to be marked as virtual.

2 Likes

1.You should use the virtual keyword only if you want inheriting contracts to be able to override functions with the same name and purpose.
2.You should use the override keyword when you want to overwrite a function in the parent contract to make it your own.
3 A contract cant have both override and virtual to allow multi level or multiple inheritance options to override its own functions as well as be able to override the parent functions it is using.

1 Like
  1. when the intent is to be enhanced/replaced by a derived contract
  2. when replacing a virtual function from one of the base contracts
  3. for answers to both 1 & 2 being true.
1 Like
  1. Whenever a function can be overridden by an inheriting contract. All interface contracts are considered that.

  2. Whenever this function is supposed to override a inherited function.

  3. Whenever you need both. This function can be overridden as well as override another one.

1 Like