Virtual and override keywords in Blockchain / Solidity - Time & Space Complexity
When using virtual and override keywords in blockchain smart contracts, it's important to understand how they affect the speed of function calls.
We want to know how the cost of calling these functions changes as the contract grows.
Analyze the time complexity of the following Solidity code snippet.
contract Base {
function foo() public virtual returns (uint) {
return 1;
}
}
contract Derived is Base {
function foo() public override returns (uint) {
return 2;
}
}
This code shows a base contract with a virtual function and a derived contract that overrides it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the overridden function.
- How many times: Each call executes once, no loops or recursion involved.
Calling a virtual or overridden function costs about the same regardless of contract size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 function call |
| 100 | 1 function call |
| 1000 | 1 function call |
Pattern observation: The cost stays constant no matter how many contracts or overrides exist.
Time Complexity: O(1)
This means calling a virtual or overridden function takes the same time no matter how many contracts are involved.
[X] Wrong: "Overriding functions makes calls slower as more contracts inherit."
[OK] Correct: The call uses a fixed lookup, so the time does not grow with the number of contracts.
Understanding how virtual and override keywords affect function call cost shows you know how smart contracts manage inheritance efficiently.
"What if the overridden function called another virtual function inside it? How would that affect the time complexity?"
Practice
virtual keyword do in a blockchain smart contract?Solution
Step 1: Understand the role of
Thevirtualvirtualkeyword marks a function so child contracts can change it.Step 2: Compare with other options
Options B, C, and D describe behaviors thatvirtualdoes not do.Final Answer:
Allows a function to be changed in child contracts -> Option DQuick Check:
virtual= allows override [OK]
- Thinking virtual makes function private
- Confusing virtual with override
- Believing virtual deletes functions
transfer in a child contract?Solution
Step 1: Identify override syntax
To replace a virtual function, useoverridekeyword in the child function.Step 2: Check options
function transfer() public override { } usesoverride, correct for overriding. function transfer() public virtual { } usesvirtualwhich allows override but does not override itself. Options C and D are incorrect keywords here.Final Answer:
function transfer() public override { } -> Option BQuick Check:
Override keyword replaces virtual function [OK]
- Using virtual instead of override to replace function
- Using final or abstract incorrectly
- Omitting override keyword
contract Parent {
function greet() public virtual returns (string memory) {
return "Hello from Parent";
}
}
contract Child is Parent {
function greet() public override returns (string memory) {
return "Hello from Child";
}
}
contract Test {
function testGreet() public returns (string memory) {
Child c = new Child();
return c.greet();
}
}What will
testGreet() return when called?Solution
Step 1: Understand virtual and override behavior
Thegreetfunction inParentis markedvirtual, allowingChildto override it.Step 2: Check which function is called
Whenc.greet()is called on aChildinstance, the overriddengreetinChildruns, returning "Hello from Child".Final Answer:
"Hello from Child" -> Option CQuick Check:
Override function runs on child instance [OK]
- Expecting parent function output
- Thinking override causes error
- Confusing virtual with abstract
contract Base {
function foo() public virtual returns (string memory) {
return "Base";
}
}
contract Derived is Base {
function foo() public returns (string memory) {
return "Derived";
}
}Solution
Step 1: Check override rules
SincefooinBaseisvirtual, the overriding function inDerivedmust useoverridekeyword.Step 2: Identify missing keyword
Thefoofunction inDerivedlacksoverride, causing a compilation error.Final Answer:
Missing override keyword in Derived's foo function -> Option AQuick Check:
Override keyword required when replacing virtual function [OK]
- Forgetting override keyword
- Thinking virtual is optional in base
- Making function private incorrectly
contract A {
function calc() public virtual returns (uint) {
return 1;
}
}
contract B is A {
function calc() public virtual override returns (uint) {
return 2;
}
}
contract C is B {
function calc() public override returns (uint) {
return 3;
}
}If you call
calc() on a C contract instance, what is the output and why?Solution
Step 1: Trace overrides in inheritance
Contract A definescalcas virtual. B overrides it and keeps it virtual. C overrides B's version without virtual.Step 2: Determine which function runs on C instance
Callingcalc()on C runs C's override, returning 3.Final Answer:
3, because C overrides B's function -> Option AQuick Check:
Most derived override runs on instance [OK]
- Thinking base virtual function runs
- Confusing virtual with override
- Expecting compilation error from multiple overrides
Ready to prove it?
Pass the test and this pattern moves unknown
