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 23 of 30 in Solidity Series
Today I Learned About Hierarchical Inheritance in Solidity.
Hierarchical Inheritance
In Hierarchical inheritance, a parent contract has more than one child contracts. It is mostly used when a common functionality is to be used in different places.
Example: In the below example, contract A is inherited by contract B, contract A is inherited by contract C, thus demonstrating Hierarchical Inheritance.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
// Defining parent contract A
contract A {
string internal x;
function getA() external {
x = "Hierarchical Inheritance";
}
uint256 internal sum;
function setA() external {
uint256 a = 10;
uint256 b = 20;
sum = a + b;
}
}
// Defining child contract B inheriting parent contract A
contract B is A {
// Defining external function to return state variable x
function getAstr() external view returns (string memory) {
return x;
}
}
// Defining child contract C inheriting parent contract A
contract C is A {
// Defining external function to return state variable sum
function getAValue() external view returns (uint256) {
return sum;
}
}
// Defining calling contract
contract caller {
// Creating object of contract B
B contractB = new B();
// Creating object of contract C
C contractC = new C();
// Defining public function to
// return values of state variables
// x and sum
function testInheritance() public view returns (string memory, uint256) {
return (contractB.getAstr(), contractC.getAValue());
}
}
Output:
when we call the testInheritance function, the output is ("Hierarchical Inheritance", 30).
Top comments (0)