Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

solidity
inheritance
communitycreator

What is inheritance in Solidity?

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

Solidity supports smart contract inheritance.

Smart contracts in Solidity programming are groups of code. Many smart contracts can be inherited as one smart contract.

Note: Inheritance can only be performed by public and internal modifiers.

Inheritance is a way to expand the usefulness of a contract. Solidity performs both single as well as multiple inheritance.

Key points

  • A derived contract can get to all non-private internal methods and states.

  • Function superseding is permitted, provided that the given function signature remains the same.

  • In case of distinction of yield parameters, compilation will fail.

  • A super contract’s function can be triggered utilizing the super keyword or utilizing the name of the super contract.

  • In the case of multiple inheritance, function call utilizing super gives inclination to the most derived contract.

In the example below, the contract parent is inherited by the contract child to demonstrate single inheritance.

pragma solidity >=0.4.22 <0.6.0;
// Defining contract
contract parent{
uint internal sum;
function setValue() external {
uint a = 10;
uint b = 20;
sum = a + b;
}
}
contract child is parent{
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();
}
}
Single inheritance example

Explanation

  • Line 4: We define a contract.

  • Line 6: We declare the internal state variable.

  • Line 8–12: We define an external function to set the value of the internal state variable sum.

  • Line 14: We define the child contract.

  • Line 16-19: We define an external function to return the value of the internal state variable sum.

  • Line 23: We define the calling contract contract caller.

  • Line 26: We create the child contract object child cc = new child();.

  • Lines 32–33: We define a function to call setValue() and getValue() functions.

RELATED TAGS

solidity
inheritance
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring