ReasonJun

Solidity : abstract 본문

Blockchain/Solidity

Solidity : abstract

ReasonJun 2023. 10. 17. 14:24
728x90

An abstract contract in Solidity is a contract that cannot be deployed by itself. It must be inherited by other contracts. Abstract contracts are used to define the common functionality of a group of related contracts.

 

To create an abstract contract, you use the abstract keyword. For example, the following contract is abstract:

abstract contract Student {
    function major() public pure virtual returns(string memory);
}

This contract defines a single function, major(), which must be implemented by any contract that inherits from it.

To inherit from an abstract contract, you use the is keyword. For example, the following contract inherits from the Student contract:

contract MathStudent is Student {
    function major() public pure override returns(string memory){
        return "Math";
    }
}

This contract implements the major() function, which is required by the Student contract.

Abstract contracts can be used to implement a variety of design patterns, such as the template method pattern and the strategy pattern. They can also be used to create reusable libraries of functionality.

 

Here are some of the benefits of using abstract contracts in Solidity:

  • Reduced code duplication: Abstract contracts can help to reduce code duplication by allowing you to define common functionality in a single contract.
  • Increased flexibility: Abstract contracts can make your code more flexible by allowing you to create different implementations of the same functionality.
  • Improved readability: Abstract contracts can make your code more readable by making it clear which functions are required and which functions are optional.

Overall, abstract contracts are a powerful feature of Solidity that can be used to improve the quality, flexibility, and readability of your code.

Here are some tips for using abstract contracts effectively:

  • Use abstract contracts to define common functionality: Abstract contracts should be used to define the common functionality of a group of related contracts. This will help to reduce code duplication and make your code more flexible.
  • Use abstract contracts to define interfaces: Abstract contracts can also be used to define interfaces for other contracts to implement. This can help to improve the modularity and reusability of your code.
  • Document your code: Document your code to explain how abstract contracts are used. This will help other developers to understand and use your code correctly.
// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

// abstract function : A function that declares only the function name, input parameters, and output parameters, but has no content.

// abstract contract = abstract fucntion + non abstract function 

abstract contract Animal {
    function eat() public virtual returns (bytes32); 
}

// contract Lion is Animal {
//     function eat() public view returns (string memory) {
//         return "Eat";
//     }
// }

example : 

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

abstract contract System {
    uint internal version;
    bool internal errorPass;

    function versionCheck() internal virtual;
    function errorCheck() internal virtual;

    function boot() public returns(uint, bool) {
        versionCheck();
        errorCheck();
        return (version, errorPass);
    }
}

contract Computer is System {
    function versionCheck() internal override {
        version = 3;
    }

    function errorCheck() internal  override {
        errorPass = true;
    }
}

contract SmartPhone is System {
    function versionCheck() internal override {
        version = 25;
    }

    function errorCheck() internal override {
        errorPass = false;   
    }
}
728x90

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

Solidity : Difference between 'abstract' and 'interface'  (1) 2023.10.17
Solidity : interface  (0) 2023.10.17
Solidity : Overriding (virtual / override / super)  (0) 2023.10.17
Solidity : Overloading  (1) 2023.10.16
Solidity: Encapsulation  (0) 2023.10.16
Comments