Day 21 – Single Inheritance

Envoy-VC
/
100-Days-of-Solidity

100 Days of Solidity step-by-step guide to learn Smart Contract Development.

This is Day 21 of 100 in Solidity Series
Today I Learned About…


This content originally appeared on DEV Community and was authored by Vedant Chainani

GitHub logo Envoy-VC / 100-Days-of-Solidity

100 Days of Solidity step-by-step guide to learn Smart Contract Development.

This is Day 21 of 100 in Solidity Series
Today I Learned About Single Inheritance in Solidity.

Inheritance is one of the most important features of the object-oriented programming language. It is a way of extending the functionality of a program, used to separate the code, reduces the dependency, and increases the re-usability of the existing code. Solidity supports inheritance between smart contracts, where multiple contracts can be inherited into a single contract. The contract from which other contracts inherit features is known as a base contract, while the contract which inherits the features is called a derived contract. Simply, they are referred to as parent-child contracts. The scope of inheritance in Solidity is limited to public and internal modifiers only. Some of the key highlights of Solidity are:

  • A derived contract can access all non-private members including state variables and internal methods. But using this is not allowed.
  • Function overriding is allowed provided function signature remains the same. In case of the difference of output parameters, the compilation will fail.
  • We can call a super contract’s function using a super keyword or using a super contract name.
  • In the case of multiple inheritances, function calls using super gives preference to most derived contracts.

Solidity provides different types of inheritance.

Single Inheritance

In Single or single level inheritance the functions and variables of one base contract are inherited to only one derived contract.

Example: In the below example, the contract parent is inherited by the contract child, to demonstrate Single Inheritance.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

// Defining contract
contract parent {
    uint256 internal sum;

    function setValue() external {
        uint256 a = 10;
        uint256 b = 20;
        sum = a + b;
    }
}

// Defining child contract
contract child is parent {
    function getValue() external view returns (uint256) {
        return sum;
    }
}

// Defining calling contract
contract caller {
    child cc = new child();

    // Defining function to call setValue and getValue functions
    function testInheritance() public returns (uint256) {
        cc.setValue();
        return cc.getValue();
    }
}

Output:

when we call the testInheritance function, the output is 30.


This content originally appeared on DEV Community and was authored by Vedant Chainani


Print Share Comment Cite Upload Translate Updates
APA

Vedant Chainani | Sciencx (2022-06-18T16:07:28+00:00) Day 21 – Single Inheritance. Retrieved from https://www.scien.cx/2022/06/18/day-21-single-inheritance/

MLA
" » Day 21 – Single Inheritance." Vedant Chainani | Sciencx - Saturday June 18, 2022, https://www.scien.cx/2022/06/18/day-21-single-inheritance/
HARVARD
Vedant Chainani | Sciencx Saturday June 18, 2022 » Day 21 – Single Inheritance., viewed ,<https://www.scien.cx/2022/06/18/day-21-single-inheritance/>
VANCOUVER
Vedant Chainani | Sciencx - » Day 21 – Single Inheritance. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/18/day-21-single-inheritance/
CHICAGO
" » Day 21 – Single Inheritance." Vedant Chainani | Sciencx - Accessed . https://www.scien.cx/2022/06/18/day-21-single-inheritance/
IEEE
" » Day 21 – Single Inheritance." Vedant Chainani | Sciencx [Online]. Available: https://www.scien.cx/2022/06/18/day-21-single-inheritance/. [Accessed: ]
rf:citation
» Day 21 – Single Inheritance | Vedant Chainani | Sciencx | https://www.scien.cx/2022/06/18/day-21-single-inheritance/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.