Blog

Inheritance in Smart Contracts

This concept, borrowed from object-oriented programming, enables code reusability and modular design. By inheriting from a parent contract, a child contract can access and utilize its state variables, functions, and modifiers. Solidity supports multiple inheritance, which means a contract can inherit from more than one parent contract.

Here is an example demonstrating inheritance in Solidity:

The Parent contract is defined first with its own state variables and functions.

In this example, the Child contract inherits both the parentValue storage variable and the setParentValue function from the Parent contract. The child contract then demonstrates how to use the inherited functionality.

State Variables: The Child contract inherits the value state variable from the Parent contract. This means the Child has its own value variable that it can read and modify.

Functions: The Child contract inherits the setValue and getValue functions from the Parent contract. It can call these functions to set and get the value of value.