Like any OOPS language, Solidity provides Abstract contracts. Abstracts contracts are similar to abstract classes in OOPS languages such as Java and typescript.
Abstracts contracts
The abstract contract is an abstract design of a contract and the implementation contract provides the implementation of the abstract function in it.
Abstract contracts are contracts that have at least one function without implementation.
abstract Contracts are contracts that are created with abstract keywords.
Abstract functions are created with virtual keyword
Here is a syntax
pragma solidity >=0.4.0 <0.7.0;
abstract contract Animal {
function eat() public virtual returns (bytes32);
}
To create an abstract contract , You need to extend it by using its keyword
Syntax
contract DerivedContract is AbstractContract
Here is an example
pragma solidity >=0.4.0 <0.7.0;
contract Lion is Animal {
function eat() public view returns(
string memory){
return _strIn;
}(bytes32){
}
}
Notes:
- Abstract contracts created with abstract keyword
- Functions defined in Abstract without implementation created with virtual keyword
- It is not possible by creating an instance of an Abstract contracts