Hope this clears it up for you:
- Inheritance
This you use when you want your contract to bahave like another, meaning whatever methods and storage variable will belong to your new contract as if you declare it there. This can be donde by writing the contract in the same file or importing the contract from a library or the same project, for this example Im going to place them in the same file for better visibility
Example:
contract Fruit{
function isFruit() returns(uint256){
return 20;
}
}
contract Apple is Fruit{
function fruitNumber() returns(uint256){
return isFruit() + 1;
}
}
- Libraries
They have a set of methods you can rehuse but doesnt mean your new contract will behave like that library it will just borrow some methods here and there for a specific type.
Example:
library MathLib {
function multiply(uint a, uint b) public view returns (uint, address) {
return (a * b, address(this));
}
}
contract Example {
using MathLib for uint;
address owner = address(this);
function multiplyExample(uint _a, uint _b) public view returns (uint, address) {
return _a.mult(_b);
}
}
- Interface
These are abstract contracts that just include the signature of the methods, normally you use them when trying to consume a deployed contract on this example we pass the address where the contract is deployed
Example:
// Uniswap example
interface UniswapV2Factory {
function getPair(address tokenA, address tokenB)
external
view
returns (address pair);
}
interface UniswapV2Pair {
function getReserves()
external
view
returns (
uint112 reserve0,
uint112 reserve1,
uint32 blockTimestampLast
);
}
contract UniswapExample {
address private factory = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
address private dai = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
address private weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
function getTokenReserves() external view returns (uint, uint) {
address pair = UniswapV2Factory(factory).getPair(dai, weth);
(uint reserve0, uint reserve1, ) = UniswapV2Pair(pair).getReserves();
return (reserve0, reserve1);
}
}