Roadmap › Solidity & Smart Contract Development › Module: Object-Oriented Solidity
Lessons in this course (11)
5. Inheritance, interfaces, abstract contracts and libraries
Intermediate★ Must knowLesson 5 of 11
solidity
interface IPriceFeed { // no implementation, all functions external
function latestPrice() external view returns (uint256);
}
abstract contract Pausable { // can't be deployed; may have unimplemented functions
bool public paused;
modifier whenNotPaused() { require(!paused, "Paused"); _; }
function _pause() internal virtual { paused = true; }
}
contract Shop is Pausable, Ownable { // multiple inheritance, C3 linearization (right-most is most derived)
constructor() Ownable(msg.sender) {}
function pause() external onlyOwner { _pause(); }
function _pause() internal override { super._pause(); emit ShopPaused(); } // virtual/override, super
event ShopPaused();
}
library MathLib { // reusable code; internal functions are inlined
function percent(uint256 x, uint256 bps) internal pure returns (uint256) { return x * bps / 10_000; }
}
using MathLib for uint256; // attach: amount.percent(250)Functions must be marked virtual to be overridable and override when overriding. Interfaces enable calling other contracts: IERC20(token).transfer(to, amt).