Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Jamiebones
Jamiebones

Posted on

     

Abstract Contract and Interfaces in Solidity

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";}}
Enter fullscreen modeExit fullscreen mode

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;}}
Enter fullscreen modeExit fullscreen mode

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);}
Enter fullscreen modeExit fullscreen mode

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";}}
Enter fullscreen modeExit fullscreen mode

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
kassim profile image

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Javascript developer working hard to master the craft.
  • Location
    Uyo, Nigeria
  • Education
    University of Ilorin
  • Work
    Author @ Educative.io
  • Joined

More fromJamiebones

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp