- Create derived contracts with the
iskeyword. - Override functions using
virtualandoverride, and call parents withsuper. - Identify single, multi-level, hierarchical and multiple inheritance.
- Explain the order rule for multiple inheritance (linearization).
What is Inheritance?
- Inheritance lets a new (derived / child) contract reuse the state variables and functions of an existing (base / parent) contract, and extend or change them by overriding.
- Unlike Java, Solidity supports multiple inheritance: one contract can inherit from several parent contracts at the same time.
- When a derived contract is deployed, the code of all its parents is compiled into one single contract – no separate parent contracts are deployed.
Key Points
- A derived contract can access all non-private members of its parents – public and internal state variables and functions. Private members exist but cannot be accessed by the child.
- A parent function can be overridden only if it is marked
virtual; the overriding function must be markedoverrideand keep the same name, parameter types and return types (otherwise compilation fails). - A parent’s version of a function can be called with
super.f()or with the parent’s name, e.g.Parent.f(). - In multiple inheritance,
supercalls the next contract in the linearized order, giving preference to the most derived contracts.
Using the “is” Keyword
contract A { }
contract B is A { } // single inheritance: B derives from A
contract C is A, B { } // multiple inheritance: list from "most base-like" to "most derived"
The order of parents matters. Solidity uses C3 linearization and requires parents to be listed from the most base-like to the most derived. In the example, B already derives from A, so contract C is A, B is valid, while contract C is B, A fails with a “Linearization of inheritance graph impossible” error.
Types of Inheritance in Solidity
| Type | Description | Shape |
|---|---|---|
| Single | One base contract is inherited by one derived contract. | A → B |
| Multi-level | A chain: the child of one parent is itself the parent of another contract. | A → B → C |
| Hierarchical | One parent has several child contracts – useful when common functionality is needed in different places. | A → B, A → C |
| Multiple | One contract inherits from several parents. | A, B → C |
Example : Overriding and super
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Person {
string internal name;
constructor(string memory _name) {
name = _name;
}
function role() public pure virtual returns (string memory) {
return "Person";
}
function describe() public view virtual returns (string memory) {
return string.concat(name, " is a ", role());
}
}
// single inheritance + overriding
contract Student is Person {
constructor(string memory _name) Person(_name) {}
function role() public pure virtual override returns (string memory) {
return "Student";
}
}
// multi-level inheritance: Person -> Student -> Monitor
contract Monitor is Student {
constructor(string memory _name) Student(_name) {}
function role() public pure override returns (string memory) {
return "Class Monitor";
}
function describe() public view override returns (string memory) {
return string.concat(super.describe(), " (elected)"); // calls Person.describe()
}
}
new Monitor("Asha").describe() returns “Asha is a Class Monitor (elected)”.
Key Takeaways
- Use
isto inherit; children access public and internal members, not private ones. - Override only
virtualfunctions, marking the new versionoverride; usesuperto call the parent. - List multiple parents from most base-like to most derived (C3 linearization).
The PDF E-Notes are the earlier printable version of this chapter. The notes on this page are the latest, revised version.
Question / Answer (15 questions)
is keyword.contract B is A.contract C is A, B. Solidity supports this, unlike Java.super.functionName(), which calls the next contract in the linearization, or explicitly with ParentName.functionName().contract C is A, B.contract B is A(5), or in its own constructor header, e.g. constructor(string memory n) Person(n) {}.MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.
- Solidity Team. (n.d.). Contracts (creating contracts, visibility and getters, function modifiers, constant and immutable, functions, events, custom errors, inheritance, abstract contracts, interfaces, libraries). Solidity Documentation. https://docs.soliditylang.org/en/latest/contracts.html
- OpenZeppelin. (n.d.). OpenZeppelin Contracts Documentation. https://docs.openzeppelin.com/contracts/
- codedamn. (n.d.). Solidity functions. https://codedamn.com/news/solidity/solidity-functions
- Modi, R. (2018). Solidity Programming Essentials: A beginner’s guide to build smart contracts for Ethereum and blockchain. Packt Publishing. ISBN 978-1-78883-138-3.