DEV Community

Cover image for Inheritance in Solidity
Shlok Kumar
Shlok Kumar

Posted on

Inheritance in Solidity

Solidity supports inheritance between smart contracts, where multiple contracts can be inherited into a single contract.

Solidity supports inheritance between smart contracts, where multiple contracts can be inherited into a single contract. There are four types of inheritance in Solidity: single, multi-level, hierarchical, and multiple. Single inheritance is when one contract is inherited by another contract. Multi-level inheritance is similar to single inheritance but has levels of the relationship between the parent and child. Hierarchical inheritance is when a parent contract has more than one child contract. Multiple inheritances is when a single contract inherits from multiple contracts at the same time.

Developers can extend a contractor's attributes and properties to their derived contracts using Solidity's inheritance feature. They can also modify these aspects in the derived contract via a process known as overriding. A derived contract can access all non-private members including internal methods and state variables. However, using this is not allowed. In case of multiple inheritances, function call using super gives preference to the most derived contract.

Solidity constructors are optional. If not set, contracts have a default constructor. Developers can indicate constructor arguments in two ways. Inheritance marks several associated contracts with a parent-child relationship. Solidity inheritance rules highly resemble Python but have some differences.

One of the most fundamental characteristics of the object-oriented programming language is the concept of inheritance. Using it to enhance the functionality of a program helps to isolate the code, eliminates dependencies, and increases the reusability of the already existing code. Solidity allows for the inheritance of smart contracts between themselves, with many contracts being able to be inherited into a single contract.

The contract from which other contracts inherit features is referred to as the base contract, and the contract from which the features are inherited is referred to as the derived contract. They are referred to as "parent-child contracts" to put it simply. Unlike other programming languages, inheritance in Solidity is restricted to public and internal modifiers only. Some of the most notable aspects of Solidity are as follows:

  • In addition to state variables and internal methods, a derived contract has access to all other non-private members of the contract. However, it is not permitted to do so.

  • Overriding functions are permitted as long as the function signature remains unchanged.

  • The compilation will fail if the output parameters are different from the input values.

  • The function of a super contract can be accessed through the use of a super keyword or the name of the super contract.

  • When there are numerous inheritances, function calls made with the super keyword give preference to the contracts that are the most deprived.

For more content, follow me at - https://linktr.ee/shlokkumar2303

Top comments (0)