Lesson 1 of 4 Advanced Solidity

Inheritance, Interfaces and Libraries

Solidity reuses code through inheritance, interfaces, abstract contracts and libraries. This lesson explains each mechanism, how overriding and multiple inheritance work, and when to choose one over another.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain inheritance, virtual and override.
  • Describe how C3 linearisation resolves multiple inheritance.
  • Distinguish interfaces, abstract contracts and libraries.
  • Choose the right reuse mechanism for a scenario.

Inheritance and overriding

A contract inherits from one or more base contracts with is. The derived contract gets the bases' state variables, functions, modifiers and events, and only the most derived contract is deployed, as a single piece of bytecode. To be overridden, a base function must be marked virtual, and the overriding function must be marked override.

contract Base {
    function fee() public view virtual returns (uint256) { return 100; }
}

contract Discounted is Base {
    function fee() public view override returns (uint256) {
        return super.fee() / 2;   // calls Base.fee()
    }
}

Base constructors receive arguments either in the inheritance list (is ERC20("Token", "TKN")) or in the derived constructor's header.

Scenario questions

Overriding functions

Exam skills 2.2.1

Apply the rules for overriding inherited functions.

Log in to try this activity Interactive activities are free with an account.

Multiple inheritance and linearisation

Solidity supports multiple inheritance and resolves it with C3 linearisation, the same algorithm Python uses. Bases are listed from the "most base-like" to the "most derived": in contract Token is ERC20, Pausable, Ownable, the order of the list determines which implementation super reaches next. When two bases define the same function, the derived contract must override it and name both: override(ERC20, Pausable).

Interfaces, abstract contracts and libraries

The following comparison sets out the three other reuse mechanisms.

Interface
ContainsExternal function declarations, events, errors, structs and enums
DeployableNo
Typical useCalling other contracts; defining standards (IERC20)
How usedIERC20(addr).transfer(...)
Abstract contract
ContainsAny code; may leave functions unimplemented
DeployableNo
Typical useA partial base for derived contracts
How usedcontract X is AbstractBase
Library
ContainsFunctions, with no state variables
DeployableOnly if it has external or public functions
Typical useReusable helpers (math, strings, safe transfers)
How usedusing Lib for uint256; or Lib.f(...)

A library's internal functions are copied into each contract that uses them at compile time, so no separate deployment is needed. external and public library functions are deployed once and called with delegatecall, so they run in the calling contract's context and the library address is linked into the caller's bytecode.

Sort into groups

Interface, abstract contract or library?

Exam skills 2.2.1

Decide which reuse mechanism each description fits.

Log in to try this activity Interactive activities are free with an account.
Worked example

Overriding with two bases

A token inherits from ERC20 and ERC20Pausable, both of which define _update. Compilation fails with "Derived contract must override function _update". What is the fix?

Add an override in the token that names both bases and calls super:

function _update(address from, address to, uint256 value)
    internal override(ERC20, ERC20Pausable)
{
    super._update(from, to, value);
}

super follows the linearised order, so both the pause check and the balance update run.

Worked example

Interface or full source?

A contract must read balances from a deployed ERC-20 token whose source is 2,000 lines long. Does the developer need to import the token's full source?

No. An interface declaring function balanceOf(address) external view returns (uint256); is enough: the compiler needs only the function signatures to encode the call. Importing the full source would add nothing and could even deploy unnecessary code if used incorrectly.

Check yourself

Name two things an interface cannot contain.

Show answer

State variables and function implementations (also constructors). An interface declares only external functions, events, errors, structs and enums.

Exam Tips

Remember for the exam

  • Overriding needs virtual in the base and override in the derived contract.
  • Multiple inheritance uses C3 linearisation; list bases from most basic to most derived.
  • Interfaces declare external functions only; abstract contracts cannot be deployed.
  • Internal library functions are inlined; external and public ones are called with delegatecall.