Hierarchical Inheritance in Solidity
Tags: solidity, ethereum, web3, blockchain
Published: 2023-03-01 17:09:00

>Hierarchical inheritance in Solidity is when a parent contract has more than one child contracts, and it is used when a common functionality is to be used in multiple contracts

Hierarchical Inheritance in Solidity is a powerful tool for developers to create complex and reusable code. It allows contracts to be inherited from other contracts, which can be used as building blocks that make up the larger contract system. This makes it easier for developers to quickly develop new smart contracts without having to start from scratch every time.

The main advantage of using hierarchical inheritance in Solidity is its ability to reduce complexity and save development time by reusing existing code instead of rewriting it each time you need something similar but slightly different. Additionally, this type of inheritance also helps ensure that all parts are well-tested before being deployed on the blockchain network since they have already been thoroughly tested elsewhere within their parent contract or ancestor hierarchy.

Finally, hierarchical inheritance in Solidity provides an efficient way for teams to work together on large projects with multiple components involved; each team member can work independently on their part while still contributing towards the overall project goal - making sure everything works perfectly when combined at deployment. Hierarchical Inheritance truly simplifies life as a developer by providing an organized structure through which we can build our smart contracts faster than ever before.

An inheritance structure in which a parent contract has more than one child contract is known as hierarchical inheritance. It is most commonly used when a similar functionality needs to be used in multiple locations at the same time.

In the following example, contract A is inherited by contract B, and contract A is inherited by contract C, exhibiting Hierarchical Inheritance in the process of inheritance.
```
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// Defining parent contract A
contract A {

     // Declaring internal
     // state variable
     string internal x;
     
     // Defining external function
     // to set value of
     // internal state variable
     function getA() external {
           x = "Hello";
     }
     
     // Declaring internal
     // state variable
     uint internal sum;
     
     // Defining external function
     // to set the value of
     // internal state variable sum
     function setA() external {
           uint a = 10;
           uint 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 getAstring(
     ) 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(uint){
           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 returns (
	string memory, uint) {
           contractB.getA();
           contractC.setA();
		return (
		contractB.getAstring(), contractC.getAValue());
	}
}
```
For more content, follow me at - https://linktr.ee/shlokkumar2303