Solidity Object-Oriented Features: Inheritance, Interfaces, Libraries, Abstract Contracts and OpenZeppelin
Inheritance
solidity
contract Animal {
function sound() public pure virtual returns (string memory) {
return "...";
}
}
contract Dog is Animal {
function sound() public pure override returns (string memory) {
return "Woof";
}
}
ismeans inheritsvirtualallows a function to be changed,overridechanges it- Solidity supports multiple inheritance. Order matters, from general to specific
Abstract Contracts and Interfaces
| Abstract contract | Interface | |
|---|---|---|
| Has implemented functions | Can have | No |
| State variables | Allowed | Not allowed |
| Use | Shared base with some missing parts | Describes what a contract offers so others can call it |
solidity
interface IToken {
function balanceOf(address who) external view returns (uint256);
function transfer(address to, uint256 amount) external returns (bool);
}
Calling Another Contract
solidity
contract Reader {
function tokenBalance(address token, address user) public view returns (uint256) {
return IToken(token).balanceOf(user);
}
}
Libraries
A library holds reusable functions. Use it with using For to extend types, for example safe math helpers or array tools.
OpenZeppelin
- A widely used library of audited, tested contracts: tokens, access control, security tools
- Do not write your own ERC-20 or ownership logic from scratch for production. Reuse OpenZeppelin
solidity
import "@openzeppelin/contracts/access/Ownable.sol";
contract Vault is Ownable {
uint256 public fee;
constructor() Ownable(msg.sender) {}
function setFee(uint256 f) external onlyOwner {
fee = f;
}
}
Version note OpenZeppelin v5 requires passing the initial owner to
Ownable(...). Older tutorials omit it.
Practice
- Create a base contract
Shapeand childSquarethat overridesarea() - What is the difference between an interface and an abstract contract?
- Why prefer OpenZeppelin over custom code?
Conclusion
Inheritance, interfaces and libraries organize and reuse code. OpenZeppelin gives audited building blocks so you do not reinvent security-critical parts.
Quick Summary: inheritance; abstract vs interface; calling; libraries; OpenZeppelin.

