Challenge - 5 Problems
Virtual and Override Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of virtual and override in Solidity inheritance
What is the output of this Solidity contract when calling
test() on contract C?Blockchain / Solidity
pragma solidity ^0.8.0; contract A { function greet() public virtual pure returns (string memory) { return "Hello from A"; } } contract B is A { function greet() public virtual override pure returns (string memory) { return "Hello from B"; } } contract C is B { function greet() public override pure returns (string memory) { return "Hello from C"; } function test() public pure returns (string memory) { return greet(); } }
Attempts:
2 left
💡 Hint
Remember that the most derived override function is called.
✗ Incorrect
In Solidity, when a function is marked virtual, it can be overridden in derived contracts. The override keyword is required to indicate the function overrides a base function. The most derived override is called, so calling greet() in contract C returns "Hello from C".
❓ Predict Output
intermediate2:00remaining
Effect of missing override keyword in Solidity
What happens if contract B tries to override function greet() from contract A but does NOT use the override keyword?
Blockchain / Solidity
pragma solidity ^0.8.0; contract A { function greet() public virtual pure returns (string memory) { return "Hello from A"; } } contract B is A { function greet() public pure returns (string memory) { return "Hello from B"; } }
Attempts:
2 left
💡 Hint
Solidity requires explicit override keyword when overriding virtual functions.
✗ Incorrect
In Solidity 0.8+, when overriding a virtual function, the override keyword is mandatory. Omitting it causes a compilation error.
🔧 Debug
advanced2:00remaining
Identify the error in multiple inheritance with virtual and override
Given the following Solidity code, what is the cause of the compilation error?
Blockchain / Solidity
pragma solidity ^0.8.0; contract A { function foo() public virtual pure returns (string memory) { return "A"; } } contract B is A { function foo() public virtual override pure returns (string memory) { return "B"; } } contract C is A { function foo() public virtual override pure returns (string memory) { return "C"; } } contract D is B, C { function foo() public override pure returns (string memory) { return super.foo(); } }
Attempts:
2 left
💡 Hint
Consider how Solidity resolves multiple inheritance and super calls.
✗ Incorrect
In multiple inheritance, calling super.foo() is ambiguous if both B and C override foo(). Solidity requires explicit disambiguation or a linearized override.
📝 Syntax
advanced2:00remaining
Correct syntax for overriding a virtual function in Solidity
Which of the following function declarations correctly overrides a virtual function
bar() from a base contract?Blockchain / Solidity
pragma solidity ^0.8.0; contract Base { function bar() public virtual pure returns (string memory) { return "Base"; } } contract Derived is Base { // Choose the correct override syntax for bar() }
Attempts:
2 left
💡 Hint
Override keyword must be present and virtual is optional if no further override is needed.
✗ Incorrect
To override a virtual function, the override keyword is mandatory. The virtual keyword is optional if you want to allow further overrides.
🚀 Application
expert3:00remaining
Determine the output of complex virtual and override usage
Consider the following Solidity contracts. What is the output of calling
callFoo() on contract Final?Blockchain / Solidity
pragma solidity ^0.8.0; contract X { function foo() public virtual pure returns (string memory) { return "X"; } } contract Y is X { function foo() public virtual override pure returns (string memory) { return string(abi.encodePacked("Y", super.foo())); } } contract Z is X { function foo() public virtual override pure returns (string memory) { return string(abi.encodePacked("Z", super.foo())); } } contract Final is Y, Z { function foo() public override(Y, Z) pure returns (string memory) { return string(abi.encodePacked("Final", super.foo())); } function callFoo() public pure returns (string memory) { return foo(); } }
Attempts:
2 left
💡 Hint
Check the linearization order of inheritance and how super calls chain.
✗ Incorrect
Solidity uses C3 linearization for multiple inheritance. The order is Final -> Y -> Z -> X. Each foo() calls super.foo(), so the output concatenates in reverse order: Final + Z + Y + X.