Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Why inheritance promotes code reuse in Blockchain / Solidity - Visual Breakdown

Choose your learning style10 modes available
Concept Flow - Why inheritance promotes code reuse
Define Base Contract
↓
Base Contract has common code
↓
Create Derived Contract
↓
Derived Contract inherits Base
↓
Derived Contract reuses Base code
↓
Add new features in Derived
↓
Deploy and use Derived Contract
Inheritance lets a new contract use code from an existing one, so you don't rewrite shared parts.
Execution Sample
Blockchain / Solidity
contract Base {
  uint public x;
  function setX(uint _x) public {
    x = _x;
  }
}

contract Derived is Base {
  function increment() public {
    x += 1;
  }
}
Shows a base contract with a variable and setter, and a derived contract that inherits and adds a new function.
Execution Table
StepActionContractVariable xFunction CalledEffect
1Deploy BaseBase0noneBase contract created with x=0
2Call setX(10)Base0 -> 10setXx updated to 10
3Deploy DerivedDerived0noneDerived contract created, inherits x from Base
4Call setX(5)Derived0 -> 5setXInherited setX sets x to 5
5Call increment()Derived5 -> 6incrementDerived adds 1 to x
6Check x valueDerived6nonex is now 6 in Derived contract
💡 Execution stops after increment and x value check in Derived contract.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
x in Base010101010
x in Derived00566
Key Moments - 3 Insights
Why does Derived contract have access to variable x even though it is declared in Base?
Because Derived inherits Base, it gets all public and internal variables like x, as shown in steps 3 and 4 of the execution_table.
What happens when Derived calls setX? Does it use its own or Base's function?
Derived uses the inherited setX function from Base, so calling setX on Derived updates x in Derived's storage, as seen in step 4.
How does inheritance help avoid rewriting the setX function?
Inheritance lets Derived reuse setX from Base without rewriting it, saving code and reducing errors, demonstrated by step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of x in Derived after calling setX(5)?
A5
B10
C0
D6
💡 Hint
Check the 'Variable x' column at step 4 in execution_table.
At which step does the Derived contract add 1 to x?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the 'Function Called' column showing 'increment' in execution_table.
If Derived did not inherit Base, what would happen when calling setX on Derived?
AIt would update x in Derived
BIt would cause an error because setX is undefined
CIt would update x in Base
DIt would create a new variable x
💡 Hint
Inheritance provides access to Base's functions; without it, Derived lacks setX.
Concept Snapshot
Inheritance lets a contract reuse code from another.
Derived contracts get variables and functions from Base.
This avoids rewriting shared code.
Derived can add new features on top.
Use 'contract Derived is Base' syntax.
Promotes cleaner, shorter code.
Full Transcript
Inheritance in blockchain contracts means one contract (Derived) can use code from another (Base). This lets Derived access variables and functions defined in Base without rewriting them. For example, Base has a variable x and a function setX to change it. Derived inherits Base, so it can call setX and also add new functions like increment to increase x. This saves time and avoids mistakes by reusing code. The execution steps show deploying Base, setting x to 10, then deploying Derived which inherits x starting at 0. Calling setX(5) on Derived updates its x to 5, then increment adds 1 making x 6. Without inheritance, Derived would not have setX and would cause errors. Inheritance promotes code reuse by sharing common code and letting Derived add new features.

Practice

(1/5)
1. Why does inheritance promote code reuse in blockchain smart contracts?
easy
A. It allows new contracts to use existing code without rewriting it
B. It makes contracts run faster on the blockchain
C. It encrypts the contract code automatically
D. It prevents contracts from being copied

Solution

  1. Step 1: Understand inheritance concept

    Inheritance lets a new contract use code from an existing contract, avoiding rewriting.
  2. Step 2: Identify how this saves effort

    By reusing code, developers save time and reduce errors, making maintenance easier.
  3. Final Answer:

    It allows new contracts to use existing code without rewriting it -> Option A
  4. Quick Check:

    Inheritance = reuse code [OK]
Hint: Inheritance means reuse existing code easily [OK]
Common Mistakes:
  • Thinking inheritance speeds up contract execution
  • Confusing inheritance with encryption
  • Believing inheritance blocks code copying
2. Which of the following is the correct Solidity syntax to inherit from a contract named BaseContract?
easy
A. contract NewContract inherits BaseContract {}
B. contract NewContract from BaseContract {}
C. contract NewContract extends BaseContract {}
D. contract NewContract is BaseContract {}

Solution

  1. Step 1: Recall Solidity inheritance syntax

    Solidity uses is keyword to inherit from another contract.
  2. Step 2: Match syntax with options

    contract NewContract is BaseContract {} uses is correctly; others use invalid keywords.
  3. Final Answer:

    contract NewContract is BaseContract {} -> Option D
  4. Quick Check:

    Solidity inheritance uses 'is' [OK]
Hint: Remember Solidity uses 'is' for inheritance [OK]
Common Mistakes:
  • Using 'inherits' or 'extends' instead of 'is'
  • Forgetting curly braces after contract declaration
  • Mixing syntax from other languages
3. Given the following Solidity code, what will be the output of getValue() when called on Child contract?
contract Parent {
    function getValue() public pure returns (string memory) {
        return "Parent";
    }
}

contract Child is Parent {
    function getValue() public pure returns (string memory) {
        return "Child";
    }
}
medium
A. "Parent"
B. "Child"
C. Compilation error due to duplicate function
D. Runtime error

Solution

  1. Step 1: Understand function overriding in inheritance

    The Child contract overrides getValue() from Parent.
  2. Step 2: Determine which function runs on Child

    Calling getValue() on Child runs the overridden version returning "Child".
  3. Final Answer:

    "Child" -> Option B
  4. Quick Check:

    Override method runs in child contract [OK]
Hint: Child overrides parent method, so child's version runs [OK]
Common Mistakes:
  • Assuming parent method runs instead of child's
  • Thinking duplicate functions cause compile error
  • Confusing runtime errors with override behavior
4. Identify the error in this Solidity inheritance code:
contract A {
    uint public x = 5;
}

contract B is A {
    uint public x = 10;
}
medium
A. Variable x is redeclared in child contract causing shadowing
B. Missing constructor in child contract
C. Incorrect inheritance keyword used
D. No error, code is correct

Solution

  1. Step 1: Check variable declarations in parent and child

    Both A and B declare x, causing shadowing.
  2. Step 2: Understand shadowing issue

    Child's x hides parent's x, which can cause confusion or bugs.
  3. Final Answer:

    Variable x is redeclared in child contract causing shadowing -> Option A
  4. Quick Check:

    Variable redeclaration causes shadowing [OK]
Hint: Avoid redeclaring variables in child contracts [OK]
Common Mistakes:
  • Ignoring variable shadowing effects
  • Thinking constructors are mandatory for inheritance
  • Confusing inheritance keyword syntax
5. You want to create a smart contract that reuses code from two existing contracts, Token and Ownable. Which approach best promotes code reuse and avoids duplication?
hard
A. Write new code that mimics Token and Ownable functions
B. Copy all code from Token and Ownable into the new contract
C. Create a new contract that inherits from both Token and Ownable
D. Deploy Token and Ownable separately and call them externally

Solution

  1. Step 1: Understand multiple inheritance in Solidity

    Solidity supports inheriting from multiple contracts to reuse their code.
  2. Step 2: Compare options for code reuse

    Inheriting from both Token and Ownable avoids duplication and keeps code organized.
  3. Final Answer:

    Create a new contract that inherits from both Token and Ownable -> Option C
  4. Quick Check:

    Multiple inheritance promotes reuse best [OK]
Hint: Use multiple inheritance to combine existing contracts [OK]
Common Mistakes:
  • Copy-pasting code instead of inheriting
  • Rewriting code manually causing errors
  • Ignoring Solidity's multiple inheritance feature
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard