envoy1084
/
30-Days-of-Solidity
30 Days of Solidity step-by-step guide to learn Smart Contract Development.
WARNING: This repository is currently undergoing updates and revisions to incorporate the latest information and advancements in Solidity programming. Please be advised that the content may not be up-to-date or accurate during this time. We expect the updates to be completed within the next 30 days, and appreciate your patience during this process. Thank you for your understanding.
Contents
- Day 1 - Licenses and Pragma
- Day 2 - Comments
- Day 3 - Initializing Basic Contract
- Day 4 - Variables and Scopes
- Day 5 - Operators
- Day 6 - Types
- Day 7 - Functions
- Day 8 - Loops
- Day 9 - Decision Making
- Day 10 - Arrays
- Day 11 - Array Operations
- Day 12 - Enums
- Day 13 - Structs
- Day 14 - Mappings
- Day 15 - Units
- Day 16 - Require Statement
- Day 17 - Assert Statement
- Day 18 - Revert Statement
- Day 19 - Function Modifiers
- Day 20…
This is Day 22 of 30 in Solidity Series
Today I Learned About Multi-level Inheritance in Solidity.
Multi-level Inheritance
It is very similar to single inheritance, but the difference is that it has levels of the relationship between the parent and the child. The child contract derived from a parent also acts as a parent for the contract which is derived from it.
Example: In the below example, contract A is inherited by contract B, contract B is inherited by contract C, to demonstrate Multi-level Inheritance.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
// Defining parent contract A
contract A {
string internal x;
string a = "Multi";
string b = "Level";
// Defining external function to return concatenated string
function getA() external {
x = string(abi.encodePacked(a, b));
}
}
// Defining child contract B inheriting parent contract A
contract B is A {
string public y;
string c = "Inheritance";
// Defining external function to return concatenated string
function getB() external payable returns (string memory) {
y = string(abi.encodePacked(x, c));
}
}
// Defining child contract C inheriting parent contract A
contract C is B {
function getC() external view returns (string memory) {
return y;
}
}
// Defining calling contract
contract caller {
// Creating object of child C
C cc = new C();
// Defining public function to return final concatenated string
function testInheritance() public returns (string memory) {
cc.getA();
cc.getB();
return cc.getC();
}
}
Output:
when we call the testInheritance function, the output is "MultiLevelInheritance".
Top comments (0)