📖 Scenario: You are building a simple blockchain project where you want to reuse code from one contract in another. This is like inheriting traits from a parent in real life. You will create a base contract with some data and then a child contract that inherits from it and adds more features.
🎯 Goal: Build two Solidity contracts where one contract inherits from another. The child contract should access and use the data from the parent contract.
📋 What You'll Learn
Create a base contract called ParentContract with a public string variable greeting set to 'Hello from Parent'.
Create a child contract called ChildContract that inherits from ParentContract.
Add a public function getGreeting in ChildContract that returns the greeting string from ParentContract.
Deploy and call getGreeting to see the inherited greeting.
💡 Why This Matters
🌍 Real World
In blockchain, contract inheritance helps developers reuse code and build complex contracts efficiently, just like inheriting traits in real life.
💼 Career
Understanding contract inheritance is essential for blockchain developers to write clean, maintainable, and scalable smart contracts.
Progress0 / 4 steps
1
Create the base contract
Create a Solidity contract called ParentContract with a public string variable greeting set to "Hello from Parent".
Blockchain / Solidity
Hint
Use contract ParentContract {} and declare string public greeting = "Hello from Parent"; inside.
2
Create the child contract with inheritance
Create a Solidity contract called ChildContract that inherits from ParentContract.
Blockchain / Solidity
Hint
Use contract ChildContract is ParentContract {} to inherit.
3
Add a function to access inherited data
In ChildContract, add a public function called getGreeting that returns the greeting string from ParentContract.
Blockchain / Solidity
Hint
Write a function getGreeting that returns greeting using return greeting;.
4
Display the inherited greeting
Write a line to print the result of calling getGreeting() from ChildContract. Use console.log style comment to simulate output since Solidity does not print directly.
Blockchain / Solidity
Hint
Since Solidity contracts do not print directly, imagine calling getGreeting() returns "Hello from Parent".
Practice
(1/5)
1. What keyword is used in Solidity to inherit from a parent contract?
easy
A. extends
B. inherits
C. is
D. implements
Solution
Step 1: Understand Solidity inheritance syntax
In Solidity, the keyword is is used to inherit from a parent contract.
Step 2: Compare with other options
Keywords like extends, inherits, and implements are not used in Solidity for inheritance.
Final Answer:
is -> Option C
Quick Check:
Inheritance keyword = is [OK]
Hint: Remember: Solidity uses 'is' for inheritance [OK]
Common Mistakes:
Confusing Solidity with other languages like Java using 'extends'
Using 'implements' which is for interfaces
Trying 'inherits' which is not a Solidity keyword
2. Which of the following is the correct way to declare a function in a parent contract that can be overridden in Solidity?
easy
A. function foo() public virtual {}
B. function foo() public abstract {}
C. function foo() public override {}
D. function foo() public final {}
Solution
Step 1: Identify the keyword to allow overriding
In Solidity, to allow a function to be overridden in child contracts, it must be marked virtual.
Step 2: Check other options for correctness
override is used in child contracts, abstract is for abstract contracts, and final is not a Solidity keyword.
Final Answer:
function foo() public virtual {} -> Option A
Quick Check:
Allow override = virtual [OK]
Hint: Use 'virtual' in parent to allow overrides [OK]
Common Mistakes:
Using 'override' in parent instead of child
Confusing 'abstract' with 'virtual'
Using non-existent 'final' keyword
3. What will be the output of the following Solidity code when calling 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";
}
}
medium
A. Runtime error
B. "Hello from Parent"
C. Compilation error due to missing override
D. "Hello from Child"
Solution
Step 1: Understand function overriding
The Child contract overrides the greet() function from Parent using override keyword.
Step 2: Determine which function runs on Child
Calling greet() on Child runs the overridden function returning "Hello from Child".
Final Answer:
"Hello from Child" -> Option D
Quick Check:
Override function output = "Hello from Child" [OK]
Hint: Child's override function runs, not parent's [OK]
Common Mistakes:
Thinking parent function runs instead of child
Missing 'override' keyword causing compile error
Expecting runtime error without cause
4. Identify the error in the following Solidity code snippet:
contract Base {
function foo() public virtual returns (string memory) {
return "Base";
}
}
contract Derived is Base {
function foo() public returns (string memory) {
return "Derived";
}
}
medium
A. Missing 'override' keyword in Derived's foo function
B. Missing 'virtual' keyword in Base's foo function
C. Derived contract should not inherit Base
D. Function foo should be private
Solution
Step 1: Check overriding rules in Solidity
When overriding a function marked virtual in the parent, the child function must use override keyword.
Step 2: Identify missing keyword
The foo() function in Derived lacks the override keyword, causing a compilation error.
Final Answer:
Missing 'override' keyword in Derived's foo function -> Option A
Quick Check:
Override missing = compile error [OK]
Hint: Override functions must use 'override' keyword [OK]
Common Mistakes:
Forgetting 'override' keyword in child function
Thinking 'virtual' is needed in child function
Assuming inheritance itself causes error
5. Given the following contracts, what will be the output of calling 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();
}
}
hard
A. "Parent"
B. "Child"
C. "GrandChild"
D. Compilation error due to missing virtual
Solution
Step 1: Understand super keyword in Solidity inheritance
The GrandChild contract calls super.getMessage(), which refers to the immediate parent Child implementation.
Step 2: Identify which function runs
The Child contract's getMessage() returns "Child", so GrandChild returns "Child" as well.
Final Answer:
"Child" -> Option B
Quick Check:
super calls immediate parent = "Child" [OK]
Hint: super calls immediate parent function [OK]
Common Mistakes:
Thinking super calls grandparent function
Expecting 'GrandChild' string without override
Missing 'virtual' in intermediate contract causing error
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.