0
0
Blockchain / Solidityprogramming~5 mins

Multiple inheritance in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

Multiple inheritance lets a contract use features from more than one contract. This helps reuse code and combine abilities easily.

When you want a contract to have functions from two or more different contracts.
When you want to combine different behaviors like ownership and token management in one contract.
When you want to organize code better by splitting features into separate contracts and then combining them.
When you want to avoid rewriting the same code in multiple contracts.
Syntax
Blockchain / Solidity
contract Child is Parent1, Parent2 {
    // child contract code
}
The child contract inherits from multiple parent contracts by listing them separated by commas.
The order of parent contracts matters if they have functions with the same name.
Examples
Contract C inherits from both A and B. If greet() is called on C, Solidity uses the function from the first parent listed (A) by overriding the function.
Blockchain / Solidity
contract A {
    function greet() public pure virtual returns (string memory) {
        return "Hello from A";
    }
}

contract B {
    function greet() public pure virtual returns (string memory) {
        return "Hello from B";
    }
}

contract C is A, B {
    // inherits from A and B
    function greet() public pure override(A, B) returns (string memory) {
        return A.greet();
    }
}
MyToken contract inherits ownership and token features by using multiple inheritance.
Blockchain / Solidity
contract Owner {
    address public owner;
    constructor() {
        owner = msg.sender;
    }
}

contract Token {
    mapping(address => uint) public balances;
}

contract MyToken is Owner, Token {
    // combines ownership and token features
}
Sample Program

This example shows multiple inheritance with contracts A and B both having a greet function. Contract C inherits both and overrides greet to use A's version.

Blockchain / Solidity
pragma solidity ^0.8.0;

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

contract B {
    function greet() public pure virtual returns (string memory) {
        return "Hello from B";
    }
}

contract C is A, B {
    // Override greet to specify which parent's greet to use
    function greet() public pure override(A, B) returns (string memory) {
        return A.greet();
    }
}

contract Test {
    function testGreet() public returns (string memory) {
        C c = new C();
        return c.greet();
    }
}
OutputSuccess
Important Notes

When multiple parents have the same function, you must override it in the child contract to avoid errors.

Use the override keyword to specify which parent function you want to use.

Multiple inheritance helps organize and reuse code but be careful with function name conflicts.

Summary

Multiple inheritance lets a contract use code from more than one contract.

It helps combine features and avoid code duplication.

When parents have same functions, override them in the child contract.