Single-level Inheritance in Solidity
Shlok Kumar
Posted on February 27, 2023
Single level inheritance in Solidity allows the derived contract to use variables and functions of the base contract
Single-level inheritance in Solidity is a powerful tool for blockchain developers. It allows them to create smart contracts that can be inherited from other existing contracts, allowing them to reuse code and save time. This type of inheritance also ensures that changes made to the parent contract are automatically applied across all child contracts, making it easier and faster for developers to update their applications as needed.
The way single-level inheritance works in Solidity is by creating an abstract contract that contains the inherited properties or functions, then declaring a new contract that inherits from the abstract one. The new child contract will have access to all of its parent’s methods and variables without needing any additional coding effort on behalf of the developer; this makes it much simpler than traditional object-oriented programming languages where each class must be written separately before being able to use its features within another class or function call. Additionally, because Smart Contracts are immutable once deployed on Ethereum’s network they cannot be changed like traditional software so using this feature helps ensure consistency between different versions of your application over time while still allowing you flexibility when updating specific parts if necessary
Single Level Inheritance provides Ethereum developers with a powerful tool for quickly building out complex applications by taking advantage of existing code libraries while ensuring consistency across multiple versions at runtime – thus saving both development time and money overall compared with more conventional approaches such as Object-Oriented Programming (OOP).
In single or single-level inheritance, the functions and variables of a base contract are passed on to only one derived contract, which is referred to as a single-level inheritance.
In the following example, the contract parent is inherited by the contract child to show a Single Inheritance of Contracts.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Defining contract
contract parent{
// Declaring internal
// state variable
uint internal sum;
// Defining external function
// to set value of internal
// state variable sum
function setValue() external {
uint a = 5;
uint b = 10;
sum = a + b;
}
}
// Defining child contract
contract child is parent{
// Defining external function
// to return value of
// internal state variable sum
function getValue(
) external view returns(uint) {
return sum;
}
}
// Defining calling contract
contract caller {
// Creating child contract object
child cc = new child();
// Defining function to call
// setValue and getValue functions
function testInheritance(
) public returns (uint) {
cc.setValue();
return cc.getValue();
}
}
For more content, follow me at - https://linktr.ee/shlokkumar2303
Posted on February 27, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.