What if you could change parts of your blockchain contract without breaking everything else?
Why Virtual and override keywords in Blockchain / Solidity? - Purpose & Use Cases
Imagine you are building a blockchain smart contract system where different contracts share some common functions but need to customize certain behaviors. Without special keywords, you have to rewrite entire functions for each contract or risk unexpected results.
Manually copying and changing code for each contract is slow and error-prone. If you forget to update one place, your blockchain logic might break or behave inconsistently, causing costly bugs.
The virtual keyword lets you mark functions that can be changed later, and override lets you safely replace those functions in child contracts. This makes your code clear, safe, and easy to maintain.
contract Base {
function calculate() public returns (uint) {
return 1;
}
}
contract Child is Base {
// manually copied and changed calculate()
function calculate() public returns (uint) {
return 2;
}
}contract Base {
function calculate() public virtual returns (uint) {
return 1;
}
}
contract Child is Base {
function calculate() public override returns (uint) {
return 2;
}
}You can build flexible blockchain contracts that share logic but customize behavior safely and clearly.
In a blockchain voting system, a base contract defines a voting method, but different elections override the method to apply special rules like weighted votes or time limits.
Virtual marks functions that can be changed later.
Override safely replaces those functions in child contracts.
This helps build clear, flexible, and safe blockchain code.
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
