Inheritance & Polymorphism
Solidity supports multiple inheritance using C3 linearization. It allows contracts to share and extend behavior, forming the basis of every OpenZeppelin standard contract.
Inheritance Basics
Solidity uses 'is' keyword for inheritance.
Multiple inheritance is allowed — order matters (most base to most derived, left to right).
C3 linearization determines method resolution order.
Key keywords:
- virtual: allows this function to be overridden in derived contracts
- override: declares this function overrides a parent function
- super: calls the next function in the inheritance chain
- abstract: contract has at least one unimplemented function
Constructors with inheritance:
Need to explicitly pass args to parent constructor.
Either in the 'is' clause or in the child constructor calling syntax.Common Mistakes
- Wrong inheritance order — Solidity resolves from most-derived to most-base (C3). If you get 'Linearization of inheritance graph impossible', swap the order in the 'is' clause.
- Calling parent constructor multiple times via super in diamond inheritance — super follows C3 order and calls each parent once. Don't call parent constructors manually.
- Forgetting 'virtual' on base functions — if the base function lacks 'virtual', the derived contract cannot override it (compile error).
- Overriding without 'override' keyword — the compiler requires explicit 'override' to prevent accidental shadowing.
Tip
Tip
Practice Inheritance Polymorphism in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Once deployed, smart contracts are immutable — code is law
Practice Task
Note
Practice Task — (1) Write a working example of Inheritance Polymorphism from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Inheritance Polymorphism is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready web3 code.
Key Takeaways
- Solidity supports multiple inheritance using C3 linearization.
- Wrong inheritance order — Solidity resolves from most-derived to most-base (C3). If you get 'Linearization of inheritance graph impossible', swap the order in the 'is' clause.
- Calling parent constructor multiple times via super in diamond inheritance — super follows C3 order and calls each parent once. Don't call parent constructors manually.
- Forgetting 'virtual' on base functions — if the base function lacks 'virtual', the derived contract cannot override it (compile error).