DEV Community

Cover image for Multi-level Inheritance in Solidity
Shlok Kumar
Shlok Kumar

Posted on

Multi-level Inheritance in Solidity

Multi-level inheritance involves multiple levels of parent-child relationships which allows for more complex and modular code structures

Multi-level inheritance is an important feature of the Solidity programming language. It allows developers to create complex, hierarchical structures for their smart contracts. With multi-level inheritance, a contract can be inherited from other contracts in multiple layers and levels. This allows for greater flexibility when it comes to creating custom logic and functions within a given contract structure.

The ability to use multi-level inheritance also makes Solidity more secure than some other languages since there are fewer opportunities for malicious code injection or manipulation of data within the system due to its layered approach. Additionally, this type of structure helps organize code into easily understandable sections which can be quickly understood by both humans and machines alike when debugging or making changes later on down the line if need be, done so in the future.

It is quite similar to single inheritance, with the exception that there are different levels of the relationship between the parent and the child in the case of multiple inheritances. The child contract that is generated from a parent contract likewise serves as a parent for the contract that is derived from the parent contract.

 Multi-level inheritance is demonstrated in the following example, in which contract A is inherited by contract B, and contract B is inherited by contract C.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// Defining parent contract A
contract A {

     // Declaring state variables
     string internal x;
     string a = "Oneneuron" ;
     string b = "Kids Neuron";

     // 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 {

     // Declaring state variables
     // of child contract B
     string public y;
     string c = "Tech Neuron";

     // 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 {
     // Defining external function
     // returning concatenated string
     // generated in child contract 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) {
           return cc.getC();
Enter fullscreen mode Exit fullscreen mode

For more content, follow me at -

Top comments (0)