Envoy-VC / 30-Days-of-Solidity
30 Days of Solidity step-by-step guide to learn Smart Contract Development.
This is Day 24
of 30
in Solidity Series
Today I Learned About Multiple Inheritance in Solidity.
Multiple Inheritance
In Multiple Inheritance, a single contract can be inherited from many contracts. A parent contract can have more than one child while a child contract can have more than one parent.
Example: In the below example, contract A is inherited by contract B, contract C is inheriting contract A, and contract B, thus demonstrating Multiple Inheritance.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
// Defining contract A
contract A {
string internal x;
function setA() external {
x = "Multiple Inheritance";
}
}
// Defining contract B
contract B {
uint256 internal pow;
function setB() external {
uint256 a = 2;
uint256 b = 20;
pow = 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 getPow() external returns (uint256) {
return pow;
}
}
// 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, uint256) {
contractC.setA();
contractC.setB();
return (contractC.getStr(), contractC.getPow());
}
}
Output:
when we call the testInheritance function, the output is ("Multiple Inheritance", 1024).
Top comments (0)