0
0
Blockchain / Solidityprogramming~10 mins

Virtual and override keywords in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Virtual and override keywords
Define base function
Mark function as virtual
Create derived contract
Override base function
Use override keyword
Call function via base or derived
Derived function runs if overridden
Shows how a base function marked virtual can be replaced by a derived function using override, enabling dynamic behavior.
Execution Sample
Blockchain / Solidity
contract Base {
  function greet() public virtual returns (string memory) {
    return "Hello from Base";
  }
}

contract Derived is Base {
  function greet() public override returns (string memory) {
    return "Hello from Derived";
  }
}
Defines a base contract with a virtual function and a derived contract that overrides it.
Execution Table
StepActionFunction CalledResultNotes
1Call greet() on Base instanceBase.greet()"Hello from Base"Base function runs as is
2Call greet() on Derived instanceDerived.greet()"Hello from Derived"Derived override runs
3Call greet() via Base reference to DerivedDerived.greet()"Hello from Derived"Override used due to virtual
4Remove override keyword in DerivedCompile errorErrorMust use override to replace virtual function
💡 Execution stops after demonstrating override behavior and compile error on missing override
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Function greet()Base versionBase versionDerived versionDerived versionDerived version
Key Moments - 3 Insights
Why do we need the virtual keyword in the base contract?
The virtual keyword (see Step 1 in execution_table) tells the compiler the function can be replaced in derived contracts. Without it, override is not allowed.
What happens if we forget the override keyword in the derived contract?
As shown in Step 4, the compiler gives an error because override explicitly confirms you intend to replace a virtual function.
When calling greet() via a base contract reference to a derived instance, which function runs?
Step 3 shows the derived override runs because the base function is virtual, enabling dynamic dispatch.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when calling greet() on a Base instance?
ACompile error
B"Hello from Base"
C"Hello from Derived"
DEmpty string
💡 Hint
Check Step 1 in the execution_table for the Base instance call result.
At which step does the compiler error occur if override keyword is missing?
AStep 2
BStep 3
CStep 4
DNo error
💡 Hint
See Step 4 in execution_table where missing override causes compile error.
If the base function is not marked virtual, what happens when derived tries to override?
ACompile error
BDerived function runs anyway
CBase function runs always
DRuntime error
💡 Hint
Refer to key_moments about the need for virtual keyword to allow override.
Concept Snapshot
virtual keyword marks a base function as replaceable
override keyword confirms replacement in derived contract
Calling via base uses derived function if overridden
Missing override causes compile error
No virtual means no override allowed
Full Transcript
This example shows how in blockchain smart contracts, a base contract can define a function as virtual, meaning it can be replaced by derived contracts. The derived contract uses the override keyword to replace the base function. When calling the function on a derived instance or via a base reference to derived, the overridden function runs. If override is missing, the compiler gives an error. If virtual is missing in base, override is not allowed. This enables flexible and safe function replacement in contract inheritance.