Abstract Contract
An abstract contract is one that cannot be deployed by itself. An abstract contract must be inherited by another contract. An abstract contract is somewhat similar to an interface but there exist some differences between them. An abstract contract can defined functions signature and can also have implementation for some of its functions.
// SPDX-License-Identifier: MITpragmasolidity^0.8.0;abstractcontractSayHello{uint256publicage;constructor(uint256_age){age=_age;}functiongetAge()publicvirtualviewreturns(uint256){returnage;}functionsetAge(uint256_age)publicvirtual{}functionmakeMeSayHello()publicpurereturns(stringmemory){return"Hello";}}
The above contractSayHello
has four functions of which one of themsetAge
is a function signature which an inheriting contract can choose to implement. The abstract contract above can not be deployed on its own like a normal contract. It has to be inherited. The contract has a constructor function and you can also defined state variables on an abstract contract like you will do for a normal contract.
// SPDX-License-Identifier: MITpragmasolidity^0.8.0;abstractcontractSayHello{uint256publicage;constructor(uint256_age){age=_age;}functiongetAge()publicvirtualviewreturns(uint256){returnage;}functionsetAge(uint256_age)publicvirtual{}functionmakeMeSayHello()publicpurereturns(stringmemory){return"Hello";}}contractHelloisSayHello{stringpublicname;constructor(stringmemory_name,uint256_age)SayHello(_age){name=_name;}functionsetName(stringmemory_name)public{name=_name;}functiongetName()publicviewreturns(stringmemory){returnname;}functiongetAge()publicoverridevirtualviewreturns(uint256){return67;}functionsetAge(uint256_age)publicoverridevirtual{age=_age;}}
In the solidity contract above, our contractHello
is inheriting the abstract contractSayHello
. The abstract contractSayHello
has a constructor function which the inheriting contractHello
calls in its own constructor. TheHello
contract can choose to override all the functions of the abstract contract with its own implementation but an inheriting contract like theHello
contract must at least implement one function of the abstract contract or it will be marked as abstract.
Interfaces
An interface is similar to an abstract contract as it must be inherited by another contract like the abstract contract. An interface functions visibility must be marked asexternal. It cannot have a constructor neither can it declare state variables.
sample interface implementation
// SPDX-License-Identifier: MITpragmasolidity^0.8.0;interfaceISayHello{functiongetAge()externalviewreturns(uint256);functionsetAge(uint256_age)externalviewreturns(uint256);functionmakeMeSayHello()externalviewreturns(stringmemory);}
An interface must not inherit from another contract or interface and an inheriting contract must implement all the functions of that interface.
// SPDX-License-Identifier: MITpragmasolidity^0.8.0;interfaceISayHello{functiongetAge()externalviewreturns(uint256);functionsetAge(uint256_age)external;functionmakeMeSayHello()externalviewreturns(stringmemory);}contractHelloAgainisISayHello{uint256publicage;functiongetAge()publicpureoverridereturns(uint256){return33;}functionsetAge(uint256_age)publicoverride{age=_age;}functionmakeMeSayHello()publicpureoverridereturns(stringmemory){return"Hello again";}}
The functionHelloAgain
inherits the interfaceISayHello
which must override and implement all the functions of the interface. If any of the function is not overridden, the compiler complains and ask you to mark the inheriting contract as abstract.
Difference between abstract contract and an interface
- An interface cannot have a constructor while an abstract contract can implement one.
- An interface cannot define state variables but an abstract contract can.
- An inheriting contract must implement all the functions defined in an interface while in an abstract contract the inheriting contract must implement at least one function of the abstract contract.
- An abstract contract can inherit from another contract or abstract contract while an interface cannot inherit from a contract or another interface.
Interfaces and abstract contracts are arsenals in our toolkit that we could use for the development of smart contracts.
Thanks for reading...
Top comments(1)
For further actions, you may consider blocking this person and/orreporting abuse