Multiple inheritance

Multiple inheritance in Solidity allows a contract to inherit from more than one parent contract. This is achieved using the is keyword followed by the name of each parent contract.

Example

contract A {
    uint public a;
}

contract B {
    uint public b;
}

contract C is A, B {
    uint public c;
}

In this example, the contract C is inheriting from both A and B. It inherits all the public state variables and functions from A and B. The contract C can also define its own state variables and functions.

The order of inheritance is important. When a function is called in the derived contract (C), Solidity will first look for it in C. If it is not found in C, Solidity will look for it in the order the parent contracts were inherited (A then B).

Multiple inheritance can be useful in situations where you want to combine multiple sets of functionality from different contracts into a single contract. However, it can also make the code more complex and harder to maintain, so it should be used judiciously.

Last updated