Assignment - Openzeppelin Reading

Here are my answers. Let me know if any changes should be made. Thank you!

  1. A function should have the ‘virtual’ keyword when you plan on it being inherited and overwritten by another contract
  2. A function should have the ‘override’ keyword when within an inheriting contract and is overwriting a ‘virtual’ function from the base contract
  3. A function would have both keywords when modifying a base ‘virtual’ function as well as planning on being inherited and modified by another contract
1 Like

1. When should you put the virtual keyword on a function?
The Virtual keyword should be used when a function could be overridden by an inheriting contract.

2. When should you put the keyword override on a function?
The Override keyword should be used when a function is changing the functionality of a function from an inherited contract.

3. Why would a function have both virtual and override keywords on it?
A function could have both keywords if your contract is inheriting from an interface contract. Since all functions in an interface contract are virtual, any contract inheriting from the interface could have functions that are both virtual and override.

1 Like

Hi everyone, these are my answers. Thanks!

  1. virtual keyword is used when a function is allowed to be modified by an inheriting contract.
  2. override is used in a function in an inherited contract that can modify a virtual function in the base contract.
  3. A function could have both virtual and override if it is modifying the function it’s inheriting from the base contract, and at the same time can be modified by another inheriting contract.
1 Like
  1. When should you put the virtual keyword on a function?
    When you want to allow inheriting contract to override its behavior
  2. When should you put the keyword override on a function?
    When you want to override a function.
  3. Why would a function have both virtual and override keywords on it?
    When you want both cases, overriding the parent function and allowing child to override.
1 Like
  1. When should you put the virtual keyword on a function?

The virtual keyword should be used in parent contracts for functions that can be overridden by child contracts.

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

The override keyword should be used for functions of child contracts that are overriding the version of the same function in a parent contract.

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

A function would have both keywords if it is both overriding a function in a parent contract and is able to be overridden by child contracts.

1 Like
  1. When should you put the virtual keyword on a function?
    You should put the virtual keyword if you want other contracts to be able to override your function

  2. When should you put the keyword override on a function?
    You should use the keyword override if you want to override an inherited function

  3. Why would a function have both virtual and override keywords on it?
    To allow your function to override and be overwritten

1 Like
  1. expect the derived class to override the function

  2. the derived class needs to implement the function of the base class

  3. the class needs to override the function of base class, when the function could be overridden by its derived class

1 Like
  1. Why should put it on the case that the function is meant to be overridden , to make it clear.
  2. When you are overriding a funciton your contract inherits.
  3. Because it overrides its parent`s function but can me overridden again by child contracts.
1 Like
  1. When should you put the virtual keyword on a function?
    You put the virtual Keyword on a function, when you want the function to be overriden, by a function from an inheriting contract.
  2. When should you put the keyword override on a function?
    When you want to override a virtual function from the inherited contract.
  3. Why would a function have both virtual and override keywords on it?
    If my contract is implementing an Interface which has a virtual function to be overriden and at the same time my contract is intended to be inherted an I want to give the option to override the very same function.
  1. “virtual” keyword designates functions in a contract that can be overriden in an inheriting contract
  2. “override” keyword designates a function that is overriding an inherited function.
  3. Such a function is overriding a parent function and can itself also be overriden in a subsequent inheriting contract.
1 Like

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 When you intent for other contract to inherit the functionality of your contract, you mark it virtual. This could be a Base or interface contract.

2 When you are changing the functions of an inherited contract or implementing it specifications, you must mark it override. This could be a Derived contract.

3 If a contract function is inherited from an interface or other contract, it must be mark as override, when it allowed itself to be overridden it must also be mark as virtual.

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

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

  3. Why would a function have both virtual and override keywords on it?
    Because that function has features that will be overridden by other functions and it overrides functionality from other contracts itself as well. This creates optimum functionality to interface with its parent contracts and allow child contracts to reference it easily and clearly.

1 Like

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

If you want child-contracts to be capable of overriding an inherited function

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

Whenever you want to override a function from a parent-conctract

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

Because a contract B might overwrite function x() from parent-contract A, but also wants a child-contract C to be capable of overwriting x()

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

When the function can be overridden.

  • When should you put the keyword override on a function?

When the contract inherits a function that needs to be overridden.

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

Function both overrides and can be overridden.

1 Like

When you want to allow another contract that is inheriting from the current contract to override its functionality.

When your contract is inheriting the same function from multiple base contracts.
You change the change or add some functionalities to the function.

A function may include both virtual and override keywords if that function already replaces a parent function and to be overridden by another function.

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

When you want to allow function be overriden in child contacts.

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

When you’re editing contract that inherits parent contract and you want to change or decorate parent contract function behaviour.

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

In order to override parent contract function and allow child contracts override that function as well.

2 Likes
  1. When you want a function to be overridden by the derived classes
  2. When you subclass a contract and want to put the same function which override the base function
  3. Because that function overrides the parent’s function and it can also be overridden again by another subclasses
1 Like
  1. Allows an inheriting contract to override its behaviour

  2. Function that overrides a function from a base contract

  3. It might have both if it’s necessary that it overrides a function and can be overridden by another function if needed.

2 Likes
1

A function that allows an inheriting contract to override its behavior should be virtual.

2

In a function that overrides that base function.

3

When it inherits a function from another contract but if someone inherits that contract he can override that function.

1 Like
  1. if you allow your children to override the function
  2. if you want to override a parent virtual function
  3. to let his children override his function and also to himself override parent function
1 Like