0
0
Blockchain / Solidityprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Contract inheritance
Define Base Contract
Define Derived Contract
Derived inherits Base
Deploy Derived Contract
Access Base and Derived functions
Contract inheritance lets one contract use code from another, like a child getting traits from a parent.
Execution Sample
Blockchain / Solidity
contract Base {
    function greet() public pure returns (string memory) {
        return "Hello from Base";
    }
}

contract Derived is Base {
    function greetDerived() public pure returns (string memory) {
        return "Hello from Derived";
    }
}
This code shows a Derived contract inheriting from Base, so it can use Base's greet function.
Execution Table
StepActionContractFunction CalledOutput
1Deploy Base contractBase--
2Call greet()Basegreet"Hello from Base"
3Deploy Derived contractDerived--
4Call greet() via DerivedDerivedgreet"Hello from Base"
5Call greetDerived()DerivedgreetDerived"Hello from Derived"
6Call greetDerived() via Base (error)BasegreetDerivedError: function not found
💡 Execution stops after testing inherited and own functions, showing inheritance works and limits.
Variable Tracker
VariableStartAfter Deploy BaseAfter Deploy DerivedFinal
Base contract deployedNoYesYesYes
Derived contract deployedNoNoYesYes
greet() accessible in BaseNoYesYesYes
greetDerived() accessible in DerivedNoNoYesYes
Key Moments - 3 Insights
Why can Derived call greet() even though it is defined in Base?
Because Derived inherits Base, it gets access to Base's functions, as shown in execution_table row 4.
Why can't Base call greetDerived()?
Base does not inherit Derived, so it has no access to Derived's functions, shown in execution_table row 6.
What happens when we deploy Derived contract?
Derived contract includes Base's code and its own, so both sets of functions are available, as seen in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what output does calling greet() on Derived produce?
A"Hello from Derived"
B"Hello from Base"
CError: function not found
DNo output
💡 Hint
Check execution_table row 4 where Derived calls greet()
At which step does the Derived contract get deployed?
AStep 1
BStep 5
CStep 3
DStep 6
💡 Hint
Look at execution_table row 3 for Derived deployment
If Base inherited Derived instead, what would happen when calling greetDerived() on Base?
AIt would return "Hello from Derived"
BIt would return "Hello from Base"
CError: function not found
DIt would cause a deployment error
💡 Hint
Inheritance direction controls function availability, see key_moments about Base not accessing Derived functions
Concept Snapshot
Contract inheritance lets one contract (Derived) use code from another (Base).
Syntax: contract Derived is Base { ... }
Derived can call Base's functions.
Base cannot call Derived's functions.
Useful for code reuse and extending functionality.
Full Transcript
Contract inheritance in blockchain means one contract can use code from another contract. We define a Base contract with a greet function. Then we define a Derived contract that inherits Base. When we deploy Derived, it has both its own functions and Base's functions. Calling greet() on Derived returns Base's greeting. Calling greetDerived() on Base causes an error because Base does not inherit Derived. This shows inheritance direction matters. Derived inherits Base, so it can use Base's code, but not the other way around.