What are abstract contracts in Solidity?

Overview

Contracts that contain at least one function but no implementation are known as abstract contracts. An abstract instance cannot be generated. Abstract contracts serve as the foundation for child contracts and allow them to inherit and utilize their functionalities.

There is no abstract term in Solidity for establishing an abstract contract. However, if a contract includes unimplemented functions, it becomes abstract.

Example

pragma solidity ^0.5.0;
contract Calculator {
function getResult() public view returns(uint);
}
contract Test is Calculator {
function getResult() public view returns(uint) {
uint a = 1;
uint b = 2;
uint result = a + b;
return result;
}
}

Output

Run the code above to see its output. However, you’ll get something like this:

0: uint256: 3

Explanation

Line 3: An abstract contract Calculator is formed.

Line 6: The Test contract inherits and implements all of the Calculator abstract contract’s functionalities.

Lines 7–11: All of the abstract functions implemented in the child contract are called using the child contract object.

A sample diagram on abstract contracts
A sample diagram on abstract contracts
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources