Assignment - Openzeppelin Reading

Something old, something new.

1 Like
  1. You should add virtual to a function if you want other contracts to be able to override the function, while inheriting from the contract it is declared in.

  2. Override should be used in the function declaration of inheriting contracts to indicate that these functions over-ride the parent contracts. They should also call the super. version of the function at the very beginning.

  3. If a contract both inherits from other base contracts, and may be inherited by other contracts. Virtual indicates that others may over-ride this function, while over-ride indicates that a particular use of a function is over-riding a parent contract function of the same name./

1 Like
  1. To allow another contract that is inheriting from the current contract to override its functionality

  2. To override a funcion from a parent contract.

  3. When already replaces a parent function and can still be replaced by a function in the inherited contract.

1 Like
  1. If it has to be possible to be overwritten by an inherited contract

  2. If a function need to override a function in the base contract

  3. when it needs both options

1 Like
  1. virtual: When you want to allow that ‘virtual’ function to be overridden in a child contract.
  2. override: To specify that a function in a child contract overrides the one in the parent.
  3. Because it may override another function but at the same time allows to be overridden again.
1 Like
  1. When should you put the virtual keyword on a function?
    If it should be possible for the child contract to override parent function.

  2. When should you put the keyword override on a function?
    If it’s overriding the function from parent contract.

  3. Why would a function have both virtual and override keywords on it?
    If it’s both overriding function from parent contract and allowing to be overriden.

2 Likes
  1. When should you put the virtual keyword on a function?
    When you want to allow that ‘virtual’ function to be overridden in a child contract.

  2. When should you put the keyword override on a function?
    To specify that a function in a child contract overrides the one in the parent.

  3. Why would a function have both virtual and override keywords on it?
    When it needs both options( because it may override another function but at the same time allows to be overridden again.)

2 Likes

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

I should put the virtual keyword to a function when i want to allow others to change it.

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

I should put the keyword override on a function, when i am planning to change it and it is a function inherited from another contract.

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

When a function overrides a function from a parent contract, and at the same time the function is planned to be overridden in the future.

2 Likes

1. When should you put the virtual keyword on a function?
When you want to allow that function to be overridden by another contract inheriting from it.

2. When should you put the keyword override on a function?
When you have overridden the functionality of the contract that the current function is inheriting from.

3. Why would a function have both virtual and override keywords on it?
When it has already overridden a function of the same name but can still yet be overridden by another function in a child contract.

1 Like
  1. When you are sure function will be overridden in child contract
  2. When you override function from base contract
  3. When it overrides method from base contract and will be overridden in child contract
1 Like
  1. when you allow inheriting contracts to override the function
  2. when you are changing the implementation of the function inherited
  3. because the function is both inherited from an interface and is inherited and overridden by another contract
1 Like
  1. When you want to allow a child contract’s function to override the preexisting contract’s function

  2. When you want to change details of the function from its parent contract

  3. A function would have both keywords in it if it’s changing its parent contract but also allows an inherited contract to change the function.

1 Like
  1. When should you put the virtual keyword on a function?
    When you want an inheriting contract to be able to override the function.

  2. When should you put the keyword override on a function?
    When a contract is overriding a function marked as virtual in a contract it is inheriting from.

  3. Why would a function have both virtual and override keywords on it?
    When the contract inherits a virtual function it wants to override but can also be inherited by contracts that want to override it.

1 Like
  1. When should you put the virtual keyword on a function?
    when you want an inherited contract override the function in other words make your own function in your contract
  2. When should you put the keyword override on a function?
    when you want to make your own version of the function
  3. Why would a function have both virtual and override keywords on it?
    This contract can override the function and any child contract can override it.
1 Like
  1. When you gives the option your function to be overridden

  2. If you use the same function name of an inherit contract

  3. When you want for your function 1. and 2.

1 Like
  1. We use the virtual keyword whenever we want to declare that a function needs to be overridden from a child contract.
  2. We use override when we actually implement the virtual function.
  3. override will be used to declare that the implementation will follow in the function body, and virtual will be used to allow following child contracts to re-implement/override the function.
1 Like

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

The function that overrides that base function should be marked as override

Both would be used when the function is overriding a parent function but also allowing children to override it.

1 Like
  1. When should you put the virtual keyword on a function?
    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?
    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?
    Virtual if we allow our function to be overridden by an inheriting contract with a function with the same name as ours, and we use the marker Override if our based contract/function is inheriting from a contract containing a function with the same name as our base function as it is the case of Interface (IERC20) contracts that by default all their functions are virtual.
1 Like

Q1. When should you put the virtual keyword on a function?
If we want the function in base contract to be overridden by a function in inherited contract.

Q2. When should you put the keyword override on a function?
If we want the function in inherited contract to override a function in base contract.

Q3. Why would a function have both virtual and override keywords on it?
A function in a contract will have both virtual and override keywords if it allows override in inherited contract and at the same time overrides a function in interface/parent contract.

1 Like
  1. A function that allows an inheriting contract to override its behavior has the virtual keyword
  2. A function that overrides a function on a base contract is marked as override.
  3. A function that inherits another function but can also be overridden by another function.
1 Like