Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How Does Inheritance Work in Solidity?

Blockchain
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 5, 2026
8 min read

Solidity inheritance lets one contract reuse and extend the state variables, functions, modifiers and events of another contract using the is keyword. Solidity supports multiple inheritance, so a contract can list several parents, and it resolves conflicts with C3 linearization. Functions can only be overridden when the parent marks them virtual and the child marks them override.

If you have written a few contracts already, inheritance is how you stop copying the same ownership check or pause logic into every one of them. This guide covers the syntax, overriding, constructors, super, multiple inheritance and the rules that catch beginners out. Examples target Solidity 0.8.x.

Table of Contents

  • Basic Solidity inheritance with the is keyword
  • Visibility and what a child can access
  • Overriding functions with virtual and override
  • Constructors and the order they run in
  • Multiple inheritance and the diamond problem
  • Calling a parent function directly
  • Practical guidance and common mistakes
  • Frequently Asked Questions
    • Does Solidity support multiple inheritance?
    • What is the difference between virtual and override?
    • Are parent contracts deployed separately?
    • Can a child call a private function of its parent?
  • Summary
  • Related Articles

Basic Solidity inheritance with the is keyword

A child contract is declared with is followed by the parent. The child gets every non-private member of the parent, and the compiler produces one deployable contract that contains both.

solidity

When you deploy Vault, the Ownable constructor runs first and sets owner to the deployer. Vault uses the inherited onlyOwner modifier without redefining it. Only one contract exists on chain; there is no separate parent contract deployed at another address.

Visibility and what a child can access

Inheritance interacts with visibility keywords. This is the usual source of "undeclared identifier" errors.

VisibilityVisible in child contractCallable from outside
publicYesYes
externalNo, only through this.f()Yes
internalYesNo
privateNoNo

A private member exists in storage and bytecode, but the child cannot name it. Use internal for helpers and state that subclasses are meant to use. Remember that private does not hide data: anyone can read contract storage from the chain.

Overriding functions with virtual and override

Since Solidity 0.6, overriding is explicit. The parent function must be virtual, and the child function must be marked override. Leaving either out is a compile error.

solidity

Calling describe() on a Circle returns I am a circle. Function calls are resolved against the most derived contract, which is how polymorphism works in Solidity. Rules to remember:

  • Overriding function must keep the same name and parameter types.
  • Visibility can only become more open: external to public, never the reverse.
  • State mutability can be tightened (nonpayable to view to pure), but not loosened.
  • A virtual function in the child can be overridden again by the next contract down.
  • State variables cannot be overridden by redeclaring them; a public state variable can only override an external function.

Modifiers can be virtual and overridden in the same way.

Constructors and the order they run in

Constructors are not inherited as functions, but they always run, from the most base contract to the most derived. If a parent constructor takes arguments, the child must supply them, either in the inheritance list or in its own constructor header.

solidity

Use the inheritance list form only for constants. If a value is chosen at deployment time, pass it through the child constructor. A child that skips a required parent argument must be declared abstract, a topic that goes together with interfaces.

Multiple inheritance and the diamond problem

A contract may list several parents. The order matters: list them from "most base-like" to "most derived". Solidity uses C3 linearization, the same method Python uses, to produce a single, predictable order of parents, so the diamond problem has a defined result.

solidity

Calling D.foo() emits the logs in this order:

text

super does not mean "the direct parent". It means "the next contract in the linearized order", so each contract in the diamond runs exactly once. When two parents define the same function, the child must name both in override(B, C), and the compiler refuses to compile otherwise. If you list parents in an impossible order, such as contract D is A, B when B is A, the compiler reports "Linearization of inheritance graph impossible".

Calling a parent function directly

You can call a specific parent by name instead of using super. This skips the linearized chain and calls only that implementation.

solidity

Prefer super in code that may be extended later, because direct calls can run a shared base more than once or skip a contract another parent expected to run.

Practical guidance and common mistakes

  • Keep inheritance trees shallow. Deep trees make storage layout and audits harder.
  • Do not reorder or insert parents in an upgradeable contract, since state variables from parents are laid out in linearized order and a change corrupts stored data.
  • Use well-tested parents, such as OpenZeppelin Contracts, rather than writing Ownable or ERC20 yourself.
  • Prefer composition (a state variable holding another contract's address) when you only need to call another contract, not reuse its code.
  • Avoid shadowing state variables. Since 0.6 the compiler rejects declaring a state variable with the same name as an inherited one.

Frequently Asked Questions

Does Solidity support multiple inheritance?

Yes. A contract can inherit from several contracts, contract D is B, C. Solidity linearizes the parents with C3 linearization, so every base runs once and the order is deterministic.

What is the difference between virtual and override?

virtual is placed on the parent function to allow overriding. override is placed on the child function to confirm it replaces a parent function. Both are required.

Are parent contracts deployed separately?

No. The compiler copies the inherited code into the child, and only the child is deployed. That differs from a library or an external contract call.

Can a child call a private function of its parent?

No. Private members are not visible to derived contracts. Mark the member internal if children need it.

Summary

Inheritance with is lets contracts share code and build on each other. Parent functions must be virtual and child functions override. Parent constructors run first, and super follows the C3 linearized order in multiple inheritance. Use internal for members meant for children, keep trees shallow, and favor audited base contracts.

Related Articles

  • How Do Events and Logs Work in Solidity?
  • What Are Interfaces and Abstract Contracts in Solidity?
  • Learn Blockchain: Complete Study Roadmap from Beginner to Advanced
Share

Similar posts

  • How Do Events and Logs Work in Solidity?
  • What Are Function Modifiers in Solidity and How Do You Use Them?
  • How Do Structs and Enums Work in Solidity?
  • How Do Mappings and Arrays Work in Solidity?
  • What Is the Difference Between Storage, Memory and Calldata?
  • How Do Functions and Visibility Work in Solidity?
  • What Are the Data Types in Solidity?
  • How Do You Write Your First Smart Contract in Solidity?