Contract inheritance lets you create new contracts based on existing ones. This helps you reuse code and add new features easily.
Contract inheritance in Blockchain / Solidity
contract ChildContract is ParentContract { // new or overridden code here }
Child contract inherits the greet function from Parent.contract Parent {
function greet() public pure returns (string memory) {
return "Hello from Parent";
}
}
contract Child is Parent {
// inherits greet() from Parent
}Child contract overrides the greet function to change its behavior.contract Parent {
function greet() public pure virtual returns (string memory) {
return "Hello from Parent";
}
}
contract Child is Parent {
function greet() public pure override returns (string memory) {
return "Hello from Child";
}
}C inherits from two contracts A and B, gaining both functions.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 {
// inherits foo() from A and bar() from B
}This program shows a base contract Animal with a function sound. Two contracts Dog and Cat inherit from Animal and override sound to return their own sounds. The Test contract creates instances and calls their sounds.
pragma solidity ^0.8.0; contract Animal { function sound() public pure virtual returns (string memory) { return "Some sound"; } } contract Dog is Animal { function sound() public pure override returns (string memory) { return "Bark"; } } contract Cat is Animal { function sound() public pure override returns (string memory) { return "Meow"; } } contract Test { function testSounds() public pure returns (string memory, string memory) { Dog dog = new Dog(); Cat cat = new Cat(); return (dog.sound(), cat.sound()); } }
Remember to mark functions in the parent contract as virtual if you want to allow overriding.
Use override keyword in the child contract when you change a function from the parent.
Multiple inheritance is allowed, but be careful with function name conflicts.
Contract inheritance helps reuse and extend code in blockchain contracts.
Use is keyword to inherit from one or more contracts.
Override functions with virtual and override keywords to customize behavior.
Practice
Solution
Step 1: Understand Solidity inheritance syntax
In Solidity, the keywordisis used to inherit from a parent contract.Step 2: Compare with other options
Keywords likeextends,inherits, andimplementsare not used in Solidity for inheritance.Final Answer:
is -> Option CQuick Check:
Inheritance keyword = is [OK]
- Confusing Solidity with other languages like Java using 'extends'
- Using 'implements' which is for interfaces
- Trying 'inherits' which is not a Solidity keyword
Solution
Step 1: Identify the keyword to allow overriding
In Solidity, to allow a function to be overridden in child contracts, it must be markedvirtual.Step 2: Check other options for correctness
overrideis used in child contracts,abstractis for abstract contracts, andfinalis not a Solidity keyword.Final Answer:
function foo() public virtual {} -> Option AQuick Check:
Allow override = virtual [OK]
- Using 'override' in parent instead of child
- Confusing 'abstract' with 'virtual'
- Using non-existent 'final' keyword
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";
}
}Solution
Step 1: Understand function overriding
TheChildcontract overrides thegreet()function fromParentusingoverridekeyword.Step 2: Determine which function runs on Child
Callinggreet()onChildruns the overridden function returning "Hello from Child".Final Answer:
"Hello from Child" -> Option DQuick Check:
Override function output = "Hello from Child" [OK]
- Thinking parent function runs instead of child
- Missing 'override' keyword causing compile error
- Expecting runtime error without cause
contract Base {
function foo() public virtual returns (string memory) {
return "Base";
}
}
contract Derived is Base {
function foo() public returns (string memory) {
return "Derived";
}
}Solution
Step 1: Check overriding rules in Solidity
When overriding a function markedvirtualin the parent, the child function must useoverridekeyword.Step 2: Identify missing keyword
Thefoo()function inDerivedlacks theoverridekeyword, causing a compilation error.Final Answer:
Missing 'override' keyword in Derived's foo function -> Option AQuick Check:
Override missing = compile error [OK]
- Forgetting 'override' keyword in child function
- Thinking 'virtual' is needed in child function
- Assuming inheritance itself causes error
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();
}
}Solution
Step 1: Understand super keyword in Solidity inheritance
TheGrandChildcontract callssuper.getMessage(), which refers to the immediate parentChildimplementation.Step 2: Identify which function runs
TheChildcontract'sgetMessage()returns "Child", soGrandChildreturns "Child" as well.Final Answer:
"Child" -> Option BQuick Check:
super calls immediate parent = "Child" [OK]
- Thinking super calls grandparent function
- Expecting 'GrandChild' string without override
- Missing 'virtual' in intermediate contract causing error
Ready to prove it?
Pass the test and this pattern moves unknown
