Contract inheritance in Blockchain / Solidity - Time & Space Complexity
When using contract inheritance in blockchain, it's important to know how the execution time changes as contracts grow.
We want to see how adding inherited contracts affects the work the blockchain does.
Analyze the time complexity of the following Solidity contract inheritance example.
pragma solidity ^0.8.0;
contract A {
function foo() public pure returns (uint) {
return 1;
}
}
contract B is A {
function bar() public pure returns (uint) {
return 2;
}
}
contract C is B {
function baz() public pure returns (uint) {
return 3;
}
}
This code shows three contracts where each one inherits from the previous, adding new functions.
Look for repeated actions that affect execution time.
- Primary operation: Calling functions from inherited contracts.
- How many times: Each function call runs once, and the function to call is determined at compile time, so no runtime chain checking is needed.
As more contracts are inherited, the system does not check more places at runtime to find functions because the function calls are resolved at compile time.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 contract | 1 step to execute function |
| 5 contracts | 1 step to execute function |
| 10 contracts | 1 step to execute function |
Pattern observation: The work to execute a function does not grow with the number of inherited contracts.
Time Complexity: O(1)
This means the time to find and run a function remains constant regardless of how many contracts are inherited.
[X] Wrong: "Inheritance does not affect execution time because functions are just copied over."
[OK] Correct: Actually, this is correct: function calls are resolved at compile time, so inheritance does not add runtime overhead in finding functions.
Understanding how contract inheritance affects execution helps you write efficient blockchain code and explain your design choices clearly.
"What if we used multiple inheritance with several contracts at the same level? How would the time complexity change?"
Practice
Solution
Step 1: Understand Solidity inheritance syntax
In Solidity, the keywordisis used to inherit from a parent contract.Step 2: Compare with other options
Keywords likeextends,inherits, andimplementsare not used in Solidity for inheritance.Final Answer:
is -> Option CQuick Check:
Inheritance keyword = is [OK]
- Confusing Solidity with other languages like Java using 'extends'
- Using 'implements' which is for interfaces
- Trying 'inherits' which is not a Solidity keyword
Solution
Step 1: Identify the keyword to allow overriding
In Solidity, to allow a function to be overridden in child contracts, it must be markedvirtual.Step 2: Check other options for correctness
overrideis used in child contracts,abstractis for abstract contracts, andfinalis not a Solidity keyword.Final Answer:
function foo() public virtual {} -> Option AQuick Check:
Allow override = virtual [OK]
- Using 'override' in parent instead of child
- Confusing 'abstract' with 'virtual'
- Using non-existent 'final' keyword
greet() on contract Child?
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";
}
}Solution
Step 1: Understand function overriding
TheChildcontract overrides thegreet()function fromParentusingoverridekeyword.Step 2: Determine which function runs on Child
Callinggreet()onChildruns the overridden function returning "Hello from Child".Final Answer:
"Hello from Child" -> Option DQuick Check:
Override function output = "Hello from Child" [OK]
- Thinking parent function runs instead of child
- Missing 'override' keyword causing compile error
- Expecting runtime error without cause
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 overriding rules in Solidity
When overriding a function markedvirtualin the parent, the child function must useoverridekeyword.Step 2: Identify missing keyword
Thefoo()function inDerivedlacks theoverridekeyword, causing a compilation error.Final Answer:
Missing 'override' keyword in Derived's foo function -> Option AQuick Check:
Override missing = compile error [OK]
- Forgetting 'override' keyword in child function
- Thinking 'virtual' is needed in child function
- Assuming inheritance itself causes error
getMessage() on contract GrandChild?
contract Parent {
function getMessage() public virtual returns (string memory) {
return "Parent";
}
}
contract Child is Parent {
function getMessage() public virtual override returns (string memory) {
return "Child";
}
}
contract GrandChild is Child {
function getMessage() public override returns (string memory) {
return super.getMessage();
}
}Solution
Step 1: Understand super keyword in Solidity inheritance
TheGrandChildcontract callssuper.getMessage(), which refers to the immediate parentChildimplementation.Step 2: Identify which function runs
TheChildcontract'sgetMessage()returns "Child", soGrandChildreturns "Child" as well.Final Answer:
"Child" -> Option BQuick Check:
super calls immediate parent = "Child" [OK]
- Thinking super calls grandparent function
- Expecting 'GrandChild' string without override
- Missing 'virtual' in intermediate contract causing error
Ready to prove it?
Pass the test and this pattern moves unknown
