Inheritance lets you build new code using existing code. This saves time and avoids repeating the same work.
When you want to create a new smart contract that shares features with an existing one.
When you want to add new abilities to a contract without rewriting old code.
When you want to fix or improve common code in one place and have all related contracts use the update.
When you want to organize your contracts in a clear, logical way by grouping shared code.
When you want to save gas by reusing tested and optimized code instead of copying it.
Syntax
Blockchain / Solidity
contract Parent {
// common code here
}
contract Child is Parent {
// new or extended code here
}
The is keyword shows inheritance from the parent contract.
Child contracts get all functions and variables from the parent automatically.
Examples
Dog inherits from Animal and changes the sound function.
Blockchain / Solidity
contract Animal {
function sound() public pure returns (string memory) {
return"Some sound";
}
}
contract Dog is Animal {
function sound() public pure override returns (string memory) {
return"Bark";
}
}
MyContract inherits the owner variable and constructor from Ownable.
Blockchain / Solidity
contract Ownable {
address owner;
constructor() {
owner = msg.sender;
}
}
contract MyContract is Ownable {
// MyContract can use owner variable
}
Sample Program
The Car contract inherits the move function from Vehicle. The Test contract creates a Car and calls both move and honk.
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Vehicle {
function move() public pure returns (string memory) {
return"Moving";
}
}
contract Car is Vehicle {
function honk() public pure returns (string memory) {
return"Beep beep!";
}
}
contract Test {
function testCar() public pure returns (string memory, string memory) {
Car myCar = new Car();
return (myCar.move(), myCar.honk());
}
}
OutputSuccess
Important Notes
Inheritance helps keep your code clean and easy to update.
Be careful with multiple inheritance to avoid conflicts.
Summary
Inheritance lets new contracts reuse code from existing ones.
This saves time and reduces mistakes by avoiding repeated code.
It helps organize contracts and makes updates easier.
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
Step 1: Understand inheritance concept
Inheritance lets a new contract use code from an existing contract, avoiding rewriting.
Step 2: Identify how this saves effort
By reusing code, developers save time and reduce errors, making maintenance easier.
Final Answer:
It allows new contracts to use existing code without rewriting it -> Option A
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
Step 1: Recall Solidity inheritance syntax
Solidity uses is keyword to inherit from another contract.
Step 2: Match syntax with options
contract NewContract is BaseContract {} uses is correctly; others use invalid keywords.
Final Answer:
contract NewContract is BaseContract {} -> Option D
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
Step 1: Understand function overriding in inheritance
The Child contract overrides getValue() from Parent.
Step 2: Determine which function runs on Child
Calling getValue() on Child runs the overridden version returning "Child".
Final Answer:
"Child" -> Option B
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
Step 1: Check variable declarations in parent and child
Both A and B declare x, causing shadowing.
Step 2: Understand shadowing issue
Child's x hides parent's x, which can cause confusion or bugs.
Final Answer:
Variable x is redeclared in child contract causing shadowing -> Option A
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
Step 1: Understand multiple inheritance in Solidity
Solidity supports inheriting from multiple contracts to reuse their code.
Step 2: Compare options for code reuse
Inheriting from both Token and Ownable avoids duplication and keeps code organized.
Final Answer:
Create a new contract that inherits from both Token and Ownable -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.