In Solidity, the super keyword is used to access the immediate parent’s implementation of a function that has been overridden. Instead of completely replacing the parent's logic, super allows to extend it by adding to it.
Extending Logic Example:
When you override a function, the original version in the parent contract is hidden. Using super.functionName() brings that logic back into your execution flow.
contract Parent {
uint public count;
function increment() public virtual {
count += 1;
}
}
contract Child is Parent {
function increment() public override {
super.increment(); // Calls Parent.increment() first
count *= 2; // Then add custom logic (multiply by 2)
}
}
Super vs. Direct Contract Calls
You can also call a parent directly by name (e.g., A.foo()), but there is a major difference:
super.foo()
- Follows the entire inheritance chain.
- Standard way to ensure all parent logic is executed only once.
A.foo()
- Calls only the specific contract A.
- Used when you want to bypass the inheritance order and trigger a specific parent.
Top comments (0)