Assignment - Openzeppelin Reading

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

Include ‘virtual’ when you want inheriting contracts to be able to override the function.

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

Include ‘override’ when overriding the base function.

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

This allows a function to override other base contract functions while having the ability to be overridden itself.

1 Like
  1. Virtual is used when you want to be able to override a function in an inheriting contract.
  2. Override is used to specify that you are overriding a base function from a previous contract.
  3. It would be useful to have both virtual and override in a function when you are overriding from a previous contract, and will be inheriting the contract again later.
1 Like
  1. When should you put the virtual keyword on a function?
    – When you want to allow the function to have another inheriting contract to override its behavior.

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

  3. Why would a function have both virtual and override keywords on it?
    – When implementing both virtual and override it allows overriding of the function in question as well as its inherited base function.

1 Like

When should you put the virtual keyword on a function?

When using a function that allows an inheriting contract to override its behaviour.

When should you put the keyword override on a function?

When using a function that overrides that particular base function.

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

So that one is able to write their own function to override the base function.
Also, all functions in interface contracts are automatically considered virtual, unless the virtual keyword is removed.

1 Like

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

When you want inheriting contracts to be able to override it.

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

When you want to override a function that you inherited from another contract.

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

Because it is a function inherited from another contract and overridden in the present contract. It can be inherited and overridden again.

1 Like
  1. When should you put the virtual keyword on a function?
    When we want a function be able to be overridden.

  2. When should you put the keyword override on a function?
    When we want a function be able to override.

  3. Why would a function have both virtual and override keywords on it?
    If a function
    Whenever a function should be able to do both: Be overridden and override

1 Like
  1. When should you put the virtual keyword on a function?
  • If you intend that a function can be overridden in an inheriting contract.
  1. When should you put the keyword override on a function?
  • If you are overriding a virtual function from a parent contract for reasons to add functionalities or modify the implementation.
  1. Why would a function have both virtual and override keywords on it?
  • Allows the function to be overridden in an inheriting contract at the same time it is overriding a virtual function from a inherited contract.
1 Like
  1. When you want the function to be able to override.

  2. You override when you override the virtual function

  3. This happens when the contract already inherits a virual function. You set it as override to override the inherited function. And you add virual to be able to override it again.

1 Like
  • When should you put the virtual keyword on a function?
    -When you want to allow an inheriting contract to overwrite its behavior
  • When should you put the keyword override on a function?
    -When you want a function to overwrite the base function
  • Why would a function have both virtual and override keywords on it?
    -Both allow the function to overwrite and be overwritten
1 Like
  1. You should put the virtual keyword on a function when you deem it is allowable to be overridden by an inheriting contract

  2. You should put the override keyword on a function when you want to override the declaration established in a contract you inherited from. You would essentially be re-writing the function for your own purposes.

  3. A function would have both if it can be overriden by future inheriting contacts and you would also like to override the function established in the contracts you are inheriting now.

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

  2. When should you put the keyword override on a function?
    The function that overrides the base contracts function should be marked as override.

  3. Why would a function have both virtual and override keywords on it?
    for a function that inherits from a base contract i.e. an abstract contract or interface and that can be overwritten by a contract that inherits it.

1 Like
  • 1. When should you put the virtual keyword on a function?
    If you want to enable others to override that particular function when inhereting it.
  • 2. When should you put the keyword override on a function?
    If you want to override an inhereted function.
  • 3. Why would a function have both virtual and override keywords on it?
    You might override a function of a contract but also want to enable others to do the same.
1 Like
  1. When should you put the virtual keyword on a function?

If you want the other contracts to be able to overdrive your function.

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

When you want to overdrive a function (E.G from a contract that was inherited)

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

If the function is intended to be overdriven and it should overdrive the function it inherited from, virtual and overdrive on the same line achieves this purpose.

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

on the function from base contract that thats behavior can be over ridden by function in inheriting contract.
2. When should you put the keyword override on a function?
on the function in inheriting contract that can over ride the behavior of base function .

  1. Why would a function have both virtual and override keywords on it?
    when we inheriting from base contract and want to create own functionality of specific function but also we can with keyword VIRTUAL let our own function to be overridden by another function in contract inheriting from our .
1 Like
  1. you should put the virtual keyword on a function when you want the function to be overridden by other functions in another contract.

  2. you should put the override keyword when you’re overriding the function from the base contract.

3.A function will have both the virtual and override keyword on it, if the function is overriding another function in its base contract and also want the function to be overridden by other functions.

1 Like
  1. When you want a contract to be able to be overriden from another contract.

  2. When you are already inheriting from another contract and you want to change it’s functionality.

  3. When both the previous statements are necessary.

2 Likes
  1. When a contract inheriting from the base contract should have a function override the same function in the base contract.

  2. When a function in an inheriting contract should override the same function in the base contract.

  3. So that it can override base functions as well as that other function can override that function.

2 Likes

When your intention is to permit to other contract that inherit from yours to create a function with the same name and adjustments on functionality.

When you are inheriting that function from another contract but what to change something

When it inherits and permits to inherit from other contracts.

2 Likes

1. When should you put the virtual keyword on a function?
The virtual keyword should be put on a function that you intend for children contracts to override.

2. When should you put the keyword override on a function?
The override keyword is on a function that is overriding a parent contracts virtual function. NOTE: if needing to override multiple parent contracts same function, you will use override(Parent1, Parent2, etc).

3. Why would a function have both virtual and override keywords on it?
A function using both virtual and override is a function that is overriding a parent functions virtual function, while also marking the function as overridable by children contracts.

1 Like
  1. Never, it is automatically assigned as virtual without having to use the word within the function.
  2. To override a function in an inherited contract in order to let the user use their own.
  3. Because the function is inheriting and overriding a function in the parent contract while allowing for another function to inherit from its own contract without any complications.
1 Like