0
0
Blockchain / Solidityprogramming~10 mins

Multiple inheritance in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple inheritance
Start
Define BaseContractA
Define BaseContractB
Define DerivedContract inheriting A and B
Create DerivedContract instance
Call methods from A and B
Use combined features
End
Shows how a contract inherits from two base contracts, combining their features.
Execution Sample
Blockchain / Solidity
contract A {
  function foo() public pure returns (string memory) {
    return "A";
  }
}

contract B {
  function bar() public pure returns (string memory) {
    return "B";
  }
}

contract C is A, B {
  function combined() public pure returns (string memory) {
    return string(abi.encodePacked(foo(), bar()));
  }
}
Defines two base contracts A and B, then a contract C inherits both and combines their functions.
Execution Table
StepActionContractFunction CalledReturn ValueNotes
1Deploy contract AA--Base contract A deployed
2Deploy contract BB--Base contract B deployed
3Deploy contract C inheriting A and BC--Derived contract C deployed with A and B features
4Call foo() from CCfoo()"A"Inherited from A
5Call bar() from CCbar()"B"Inherited from B
6Call combined() from CCcombined()"AB"Combines foo() and bar() results
7End---Execution complete
💡 All functions called successfully, demonstrating multiple inheritance.
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 6Final
foo() return-"A""A""A""A"
bar() return--"B""B""B"
combined() return---"AB""AB"
Key Moments - 3 Insights
How does contract C access functions from both A and B?
Contract C inherits both A and B, so it can call their public functions directly, as shown in steps 4 and 5.
What happens if A and B have functions with the same name?
Solidity requires explicit override to resolve conflicts; without it, compilation fails. This example avoids that by using different function names.
Why does combined() use abi.encodePacked to join strings?
Because Solidity strings cannot be concatenated with +, abi.encodePacked combines them into one string, shown in step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does foo() return when called from contract C?
A"AB"
B"B"
C"A"
D""
💡 Hint
Check step 4 in the execution table where foo() is called from C.
At which step does the combined() function return the concatenated string?
AStep 5
BStep 6
CStep 4
DStep 7
💡 Hint
Look at the execution table row where combined() is called.
If contract B also had a function named foo(), what would happen without explicit override?
ACompilation error due to function name conflict
BContract C would compile and use B's foo()
CContract C would compile and use A's foo()
DContract C would ignore both foo() functions
💡 Hint
Refer to the key moment about function name conflicts in multiple inheritance.
Concept Snapshot
Multiple inheritance lets a contract inherit from two or more contracts.
Syntax: contract Derived is Base1, Base2 { ... }
Derived contract can call public functions from all bases.
If functions share names, explicit override is needed.
Use combined features by calling inherited functions.
Full Transcript
This example shows multiple inheritance in blockchain smart contracts. Two base contracts A and B each have a function returning a string. Contract C inherits both and can call foo() from A and bar() from B. The combined() function in C joins these results. The execution table traces deployment and function calls step-by-step. Key points include how C accesses both base functions, the need for explicit overrides if function names clash, and string concatenation using abi.encodePacked. The visual quiz tests understanding of return values and inheritance behavior.