Learning Objectives – after this chapter you will be able to:
  • Create derived contracts with the is keyword.
  • Override functions using virtual and override, and call parents with super.
  • 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 marked override and 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, super calls 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
TypeDescriptionShape
SingleOne base contract is inherited by one derived contract.A → B
Multi-levelA chain: the child of one parent is itself the parent of another contract.A → B → C
HierarchicalOne parent has several child contracts – useful when common functionality is needed in different places.A → B, A → C
MultipleOne 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 is to inherit; children access public and internal members, not private ones.
  • Override only virtual functions, marking the new version override; use super to call the parent.
  • List multiple parents from most base-like to most derived (C3 linearization).
Download | Print      E-Notes

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)

Answer : Inheritance allows a derived contract to reuse the state variables and functions of one or more base contracts and to extend or override them. It is declared with the is keyword.

Answer : In single (single-level) inheritance, the functions and variables of one base contract are inherited by only one derived contract, e.g. contract B is A.

Answer : In multiple inheritance a single contract inherits from more than one parent contract, e.g. contract C is A, B. Solidity supports this, unlike Java.

Answer : A chain of inheritance in which a child contract is itself the parent of another contract, e.g. Person → Student → Monitor.

Answer : One parent contract has several child contracts, e.g. Vehicle → Car and Vehicle → Bike. It is used when common functionality is shared by different contracts.

Answer : All non-private members: public and internal state variables and functions. Private members cannot be accessed by the child.

Answer : virtual marks a function that derived contracts are allowed to override. override marks the function in the derived contract that replaces it. Both are required for overriding.

Answer : The function name, parameter types and return types. If they differ, it is treated as a different function or causes a compilation error.

Answer : With super.functionName(), which calls the next contract in the linearization, or explicitly with ParentName.functionName().

Answer : C3 linearization is the algorithm Solidity uses to determine a single order of base contracts for multiple inheritance. Parents must be listed from most base-like to most derived; an order that cannot be linearized causes a compile error.

Answer : Because A is more base-like than B but is listed after it. Solidity reports “Linearization of inheritance graph impossible”. The valid order is contract C is A, B.

Answer : One. The code of all parents is compiled into the single derived contract.

Answer : The version in the next contract of the linearized order, giving preference to the most derived contracts rather than simply the parent named first.

Answer : Either in the inheritance list, e.g. contract B is A(5), or in its own constructor header, e.g. constructor(string memory n) Person(n) {}.

Answer : “Asha is a Class Monitor (elected)”. Monitor’s describe() calls super.describe(), which runs Person.describe(); that calls role(), which resolves to the most derived override in Monitor.
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.

Q1. Which keyword declares that contract B inherits from contract A?

Show Answer

Correct Answer : C. is
Solidity uses contract B is A.

Q2. To allow a function to be overridden, the parent function must be marked:

Show Answer

Correct Answer : B. virtual
virtual permits overriding; override is used in the child.

Q3. A parent contract with several child contracts is:

Show Answer

Correct Answer : D. Hierarchical inheritance
One parent, many children = hierarchical.

Q4. Which members of a parent are NOT accessible in a child contract?

Show Answer

Correct Answer : B. private
Private members are visible only in the defining contract.

Q5. If B inherits from A, which declaration is valid?

Show Answer

Correct Answer : D. contract C is A, B
Parents are listed from most base-like to most derived.

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.

  1. 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
  2. OpenZeppelin. (n.d.). OpenZeppelin Contracts Documentation. https://docs.openzeppelin.com/contracts/
  3. codedamn. (n.d.). Solidity functions. https://codedamn.com/news/solidity/solidity-functions
  4. 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.