DEV Community

Cover image for Multiple Inheritance in Solidity
Shlok Kumar
Shlok Kumar

Posted on

Multiple Inheritance in Solidity

Solidity supports multiple inheritance, which allows a single contract to inherit from multiple contracts at the same time

Multiple inheritance is a powerful feature of Solidity, the programming language used to develop smart contracts on Ethereum. It allows for code reuse and better organization of complex projects. With multiple inheritance, developers can create classes that inherit from more than one parent class. This makes it easier to implement complicated logic or incorporate existing libraries into their projects without having to write all the code from scratch.

The primary benefit of using multiple inheritance in Solidity is that it enables developers to avoid writing duplicate code by inheriting common functions and properties from different parent classes instead of rewriting them each time they need them for a new project or contract type. By leveraging this feature, developers can save time while also ensuring that any changes made in one part will be reflected throughout their entire project due to its shared nature across various components which reduces maintenance costs as well as potential errors caused by redundant coding efforts.

In addition, multiple inheritance can help improve readability and maintainability since related functionality can be grouped under a single class hierarchy instead of being spread out over many separate files with no clear link between them making navigation through large projects a much simpler task overall. Ultimately, multiple Inheritance provides an efficient way for Solidity users to organize complex applications within manageable hierarchies which leads to improved scalability - allowing teams to work faster on larger-scale initiatives without compromising quality standards along the way.

Multiple Inheritance in Solidity allows a single contract to inherit from multiple contracts at the same time. This is achieved by using the 'is' keyword and declaring functions as virtual or overriding them with the 'override' keyword. Solidity uses C3 linearization to resolve multiple inheritance. One contract can be inherited from multiple contracts under Multiple Inheritance. A parent contract may include more than one child, whereas a child contract may include more than one parent.

Example

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

// Defining contract A
contract A {

     // Declaring internal
     // state variable
     string internal x;

     // Defining external function
     // to set value of
     // internal state variable x
     function setA() external {
           x = "Blockchain";
     }
}

// Defining contract B
contract B {
     // Declaring internal
// state variable
     uint internal mul;

     // Defining external function
     // to set value of internal
     // state variable pow
     function setB() external {
           uint a = 2;
           uint b = 20;
           mul = a ** b;
           
     }
}

// Defining child contract C
// inheriting parent contract
// A and B
contract C is A, B {

// Defining external function
// to return state variable x
function getStr(
) external returns(string memory) {
           return x;
     }

     // Defining external function
     // to return state variable pow
     function getMul(
     ) external returns(uint) {
           return mul;
     }
}

// Defining calling contract
contract caller {

     // Creating object of contract C
     C contractC = new C();

     // Defining public function to
     // return values from functions
// getStr and getPow
     function testInheritance(
     ) public returns(string memory, uint) {
           contractC.setA();
           contractC.setB();
           return (
           contractC.getStr(), contractC.getMul());
     }
} 
Enter fullscreen mode Exit fullscreen mode

Example with function overriding

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

/* Graph of inheritance
    A
   / \
  B   C
 / \ /
F  D,E

*/

contract A {
    function foo() public pure virtual returns (string memory) {
        return "A";
    }
}

// Contracts inherit other contracts by using the keyword 'is'.
contract B is A {
    // Override A.foo()
    function foo() public pure virtual override returns (string memory) {
        return "B";
    }
}

contract C is A {
    // Override A.foo()
    function foo() public pure virtual override returns (string memory) {
        return "C";
    }
} 
Enter fullscreen mode Exit fullscreen mode

For more content, follow me at - https://linktr.ee/shlokkumar2303

Top comments (0)