Skip to content
Back to Curriculum
ADVANCED

Inheritance

Solidity supports multiple inheritance (C++ style) with C3 linearization. Contracts can inherit functionality from other contracts using the `is` keyword. This is the backbone of reusability (e.g., `is ERC20`).

Syntax Structure

Example.sol
Loading...

Key Components

  • Boilerplate structure required by the Solc compiler.
  • Placement of state variables versus functions.

Practical Implementation

Multiple inheritance and the 'super' keyword.

Practical.sol
Loading...

Core Concepts

C3 Linearization

The specific order in which parent contracts are initialized and searched for functions. Right-to-Left in declarations.

virtual/override

Explicit keywords required to modify parent behavior (safety feature).

super

Calls the function one level up the inheritance tree.

Mental Model

The DNA Tree

Inheritance is like genetics. The Child contract gets features from its Parent. If the Parent has blue eyes, the Child has blue eyes, unless the Child explicitly 'overrides' that gene. With multiple parents, order matters—you get the features of the last parent listed most strongly.

Under the Hood

Inheritance doesn't exist at runtime! The compiler flattens everything. It takes the code from Parent A, Parent B, and Child, and mashes it into ONE giant bytecode blob. `super` calls are just internal jumps.

JUMPJUMPDEST

Gas Analysis

Inheritance is free at runtime but costs at deployment.

  • • Inheriting a huge contract increases your deployment size, even if you only use one function.
  • • Internal function calls between inherited contracts are cheap (simple jumps).

Security Risks

Diamond Problem and Shadowing.

  • ! State Variable Shadowing: (Disallowed in 0.6+) Declaring a variable with the same name as a parent.
  • ! Constructor Order: Base constructors are executed following linearization rules, not necessarily how you'd expect.

Common Use Cases

Using standard libraries (OpenZeppelin).
Splitting large contracts into modules.
Overriding default behavior (`virtual` / `override`).

Interactive Visualization

See exactly how the EVM handles this code in memory, storage, and the stack.

Interactive Simulation
0 / 0
Loading...
Type Solidity code (v0.8.26) and click Run. Compiler loads on first use (~2MB).

Click Run to execute the simulation

Then use the step controls to navigate