
Inheritance is the most important feature of object-oriented programming. It is used to decouple the code, reduce the dependency, and increase the re-usability of existing code. Solidity provides inheritance between smart contracts; you can write multiple contracts and inherit into a single one. The contract from where other contracts inherit the features called base contract; the contract which inherits the features known as a derived contract—in a slightly different language, people often referred it as a parent and child contract, respectively.
In Solidity, inheritance scope is limited with public and internal access modifiers, meaning that only public and internal properties of a base contract are available to the derived contract.
The Solidity compiler compiles all base contracts code into a single contract and that single contract goes to the blockchain.
How to use inheritance in Solidity?
Solidity has the “is” keyword to inherit the base contract to the derived contract.
- contract baseContract {
- //parent contract
- }
- contract derivedContract is baseContract {
- //child contract
- }
- pragma solidity >=0.4.22 <0.6.0;
- contract parent{
- uint internal id;
- function setValue(uint _value) public{
- id = _value;
- }
- }
- contract child is parent{
- function getValue() public view returns(uint){
- return id;
- }
- }

Solidity supports different types of inheritance
- Single Inheritance
- Multi-level Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
Single Inheritance
Single or single-level inheritance helps to use variables and functions of the base contract to the derived contract.

Let’s change our previous code snippet and try to understand the usage of the single inheritance.
- pragma solidity >=0.4.22 <0.6.0;
- contract parent{
- uint internal id;
- function setValue(uint _value) external {
- id = _value;
- }
- }
- contract child is parent{
- function getValue() external view returns(uint) {
- return id;
- }
- }
- contract caller {
- child cc = new child();
- function testInheritance(uint _input) public returns (uint) {
- cc.setValue(_input);
- return cc.getValue();
- }
- }
Things to notice -
- Functions setValue and getValue are defined as external.
- Added third contract "caller" to call functions of the base and parent contract.
- Created an object of the child contract named "cc".
- Added method "testInheritance", using by we will set and get the value of the id variable.
Now, let's jump to the Remix IDE, paste our example code, and deploy the "caller" contract. Once it is deployed, you should see the screen as below.

Add value—15 in the upper screen—to the method “testInheritance”, hit the method, and check the terminal, you should see the same value that you passed to the method. Here, the value of the variable “id” has been updated, and that we have accessed it from the child contract.
Multi-level Inheritance
Multi-level inheritance is similar to the single level, but the difference is, it has levels of parent-child relationships. The child contract—derived from a parent contract—also acts as a parent contract of another contract. Take a look at the following diagram:







Join the conversation! Your thoughts help the community grow.