ReasonJun

Solidity : Inheritance (is, override / Multiple) 본문

Blockchain/Solidity

Solidity : Inheritance (is, override / Multiple)

ReasonJun 2023. 10. 16. 23:07
728x90

Inheritance in Solidity is a way to create new contracts that reuse and extend the functionality of existing contracts. This can be useful for reducing code duplication, creating reusable libraries, and building complex systems from smaller components.

 

is

To inherit from another contract, you use the is keyword. For example, the following contract inherits from the ERC20 contract:

contract MyToken is ERC20 {
  // ...
}

This means that the MyToken contract will have all of the same functions and state variables as the ERC20 contract, plus any additional functions and state variables that you define in the MyToken contract.

 

example :

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract Student {
    string public schoolName = "The University of Solidity";
    // not change according to UniStudent
}

contract UniStudent is Student {
    function changeSchoolName() public {
        schoolName = "The University of Blockchain";
    }

    function getSchoolName() public view returns(string memory) {
        return schoolName; 
    }
}

example :

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract Student {
    string public schoolName = "The University of Solidity";
    string public major;
    constructor(string memory _major) {
        major = _major;
    }
}

contract ArtStudent is Student("Art") {
     // major : Art
     // schoolName : The University of Solidity
}

contract MusicStudent is Student {
    string internal degree = "Music";
    constructor() Student(degree){

    }
    // major : Music
    // schoolName : The University of Solidity
}

contract MathStudent is Student {
    constructor(string memory _major) Student(_major) {

    }
    
}

override

You can also override functions from the base contract by redeclaring them with the override keyword. This allows you to customize the behavior of the function for your specific needs.

 

For example, the following code overrides the transfer() function from the ERC20 contract to add a fee:

contract MyToken is ERC20 {
  uint256 public fee;

  function transfer(address recipient, uint256 amount) override public returns (bool) {
    require(amount >= fee, "Insufficient funds");

    _transfer(msg.sender, recipient, amount - fee);
    return true;
  }
}

Solidity also supports multiple inheritance, which means that a contract can inherit from multiple base contracts. This can be useful for combining the functionality of multiple contracts into a single contract.

 

However, multiple inheritance can also be complex to manage, so it is generally best to avoid it unless you are sure that you need it.

Here are some of the benefits of using inheritance in Solidity:

  • Reduced code duplication: Inheritance allows you to reuse code from existing contracts, which can save you a lot of time and effort.
  • Increased reusability: You can create reusable libraries of functionality that can be inherited by other contracts.
  • Modular design: Inheritance can help you to design complex systems from smaller, more manageable components.

Overall, inheritance is a powerful feature of Solidity that can be used to improve the quality, readability, and maintainability of your code.

 

Multiple Inheritance

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract ArtStudent {
    string public schoolName = "The solidity University";
    uint public schoolHours = 5;
}

contract PartTimer {
    string public  workPlace = "A pizza shop";
    uint public workingHours = 6;
}

contract Alice is ArtStudent, PartTimer {
    uint public totalHours = schoolHours + workingHours;
}

Shadowing inherited state variables

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

// Unlike functions, state variables cannot be redefined by redeclaring them in a subcontract.

contract A {
    string public name = "Contract A";

    function getName() public view returns (string memory) {
        return name;
    }
}

// Shadowing is disallowed in Solidity 0.6
// This will not compile
// contract B is A {
//  string public name = "Contract B";
// }

// Shadowing inherited state variables: How to properly redefine inherited state variables

contract C is A {
    constructor() {
        name = "Contract C";
    }
}

 

728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : Overloading  (1) 2023.10.16
Solidity: Encapsulation  (0) 2023.10.16
Solidity : Immutable / Constant  (0) 2023.10.16
Solidity : constructor  (0) 2023.10.16
Solidity : Event  (0) 2023.10.16
Comments