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.
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.
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.
Inheritance interacts with visibility keywords. This is the usual source of "undeclared identifier" errors.
| Visibility | Visible in child contract | Callable from outside |
|---|---|---|
public | Yes | Yes |
external | No, only through this.f() | Yes |
internal | Yes | No |
private | No | No |
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.
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.
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:
external to public, never the reverse.nonpayable to view to pure), but not loosened.virtual function in the child can be overridden again by the next contract down.public state variable can only override an external function.Modifiers can be virtual and overridden in the same way.
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.
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.
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.
Calling D.foo() emits the logs in this order:
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".
You can call a specific parent by name instead of using super. This skips the linearized chain and calls only that implementation.
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.
Ownable or ERC20 yourself.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.
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.
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.
No. Private members are not visible to derived contracts. Mark the member internal if children need it.
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.