Introduction to Abstract contracts
Like any OOPS language, Solidity provides abstract contracts. Abstract contracts are similar to abstract classes in OOPS languages such as Java and Typescript. There is no abstract term in Solidity for establishing an abstract contract. However, if a contract includes unimplemented functions, it becomes abstract.
- Instance of an abstract cannot be created
- Derived contract will implement the abstract function and use them as and when required
- Contracts that have at least one function without its implementation is abstract
- Also in the case when we don’t intend to create a contract directly we can consider the contract to be abstract
- Abstract contracts are used as base contracts so that the child contract can inherit and utilize its functions
- Any derived contract inherited from it should provide an implementation for the incomplete functions, and if the derived contract is also not implementing the incomplete functions then that derived contract will also be marked as abstract.
- The abstract contract is an abstract (incomplete / hypothetical) design of a contract. The implementation of contract provides the implementation of the abstract function in it. Once the abstract contract is created, you need to extend it by using is keyword.
pragma solidity ^0.5.0;
contract CalciAbstract {
function getResult() public view returns(uint);
}
contract Compute is CalciAbstract {
function getResult() public view returns(uint) {
uint a = 1;
uint b = 2;
uint result = a + b;
return result;
}
}
Comments
Post a Comment