In Solidity, an interface declares function signatures with no implementation, and an abstract contract is a contract that may mix implemented and unimplemented functions. Neither can be deployed on its own. Interfaces describe how other contracts can be called, while abstract contracts provide a partly finished base that child contracts complete.
Use the solidity interface abstract contract pair of tools when you want to program against a shape instead of a concrete contract, for example calling any ERC-20 token without importing its code. Examples target Solidity 0.8.x.
An interface is declared with the interface keyword. It contains only function signatures, and it has strict limits that keep it a pure description of an external API.
Interface rules:
external, and they are implicitly virtual.I, such as IERC20.A contract implements an interface with is. It must provide every function, or the compiler asks you to mark the contract abstract.
Since Solidity 0.8.8 the override keyword is optional when a function only implements an interface function, but writing it makes intent clear. Any contract that matches the function selectors works with IGreeter, even if it does not formally inherit from it.
The most common use of an interface is to call a contract that is already deployed. Wrap its address in the interface type and call the functions.
The compiler encodes the call using the function selector from the interface, and it checks that the target address has code. The interface does not verify that the contract really behaves as declared, so only call addresses you trust. Some real tokens do not return a boolean from transfer, which is why production code uses a wrapper such as OpenZeppelin SafeERC20.
An abstract contract uses the abstract keyword. It can have state variables, constructors, modifiers and fully implemented functions, plus at least one function without a body marked virtual.
Payable supplies the shared purchase logic, and FixedPriceShop fills in the missing price(). This is the template method pattern: the abstract base defines the steps, the child provides the variable part. Trying to deploy Payable directly fails at compile time, and a child that forgets price() must itself be abstract.
| Feature | Interface | Abstract contract |
|---|---|---|
| Function bodies | Not allowed | Allowed, mixed with unimplemented ones |
| State variables | Not allowed | Allowed |
| Constructor | Not allowed | Allowed |
| Function visibility | external only | Any |
| Inherit from | Interfaces only | Contracts, abstract contracts and interfaces |
| Typical purpose | Describe an external API | Share code, leave hooks to children |
Choose an interface when you only need the shape of an API, for example to call other contracts or to define a standard such as ERC-20. Choose an abstract contract when several contracts share real code and state but differ in a few functions.
Each interface has an ID: the XOR of all its function selectors, available as type(IGreeter).interfaceId. The ERC-165 standard uses it so a contract can announce which interfaces it supports through supportsInterface(bytes4). NFT marketplaces and wallets use this to detect ERC-721 or ERC-1155 support. OpenZeppelin provides ERC165 as an abstract contract for this purpose, a good real example of the pattern.
No. Both are incomplete by design. Only a contract that implements every function can be deployed.
Yes. An interface can inherit from other interfaces, but not from contracts. It gains their functions and you declare them with is.
No. An interface with just the functions you need is enough, as long as the selector and parameter types match.
Interfaces produce no bytecode on their own. Gas is spent only on the calls that use them.
Interfaces describe an external API with external function signatures only, and they are the standard way to call other contracts. Abstract contracts mix implemented code with unimplemented virtual functions and cannot be deployed until a child completes them. Pick interfaces for shapes and abstract contracts for shared code, and use ERC-165 interface IDs when contracts need to advertise what they support.