Inheritance (Smart Contracts)
1 min read
Pronunciation
[in-hair-uh-tuhns smart kon-trakts]
Analogy
Inheritance is like a family recipe passed down—you get all the ingredients and steps from your parent plus your own additions.
Definition
Key Points Intro
Contract inheritance promotes reuse via:
Key Points
Single & multiple: Supports linearized multiple inheritance (C3 linearization).
`is` keyword: `contract B is A` imports A’s definitions.
Overriding: Child can override parent functions with `virtual`/`override`.
Modifiers inheritance: Child contracts inherit and can combine modifiers.
Example
```
contract Ownable { address owner; modifier onlyOwner { ... } }
contract MyToken is Ownable { function mint() external onlyOwner { ... } }
```
Technical Deep Dive
Solidity uses C3 linearization to compute a consistent Method Resolution Order. Parent constructors can be invoked explicitly in child constructors. Virtual functions in parents must be marked `virtual`; overrides in children require `override` keyword. Storage layout merges parent slots first.
Security Warning
Incorrect ordering in multiple inheritance can introduce vulnerabilities; follow the “most base-like to most derived” order.
Caveat
Complex inheritance trees can obfuscate logic; prefer composition for clarity.
Inheritance (Smart Contracts) - Related Articles
No related articles for this term.