Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Jamiebones
Jamiebones

Posted on

     

Inheritance In Solidity (My Web3 Journey ) part 5

Solidity supports object oriented programming paradigm of inheritance. Inheritance means adding extra functionality to a contract by inheriting them from another contract.

We could want some functionality in our contract and instead of defining and implementing them from scratch, we could just inherit it from a base or parent contract.

Inheritance of a contract in solidity is done by theis keyword. Solidity supports multiple inheritance; meaning you can inherit from more than one parent contract.

Inheritance tips

  • Inheritance is done by using theis keyword to inherit from a base contract.

  • A Function in the parent contract that should be overridden by the child contract should be declared as virtual.

  • A child contract that will override the parent function must use the keyword override.

  • Order of inheritance is important.

pragmasolidity^0.8.4;interfaceBonny{//we defined an interface to force contract inheriting that interface to implement the methods define n the interfacefunctiongethelp()externalviewreturns(stringmemory);}contractBaseContract{//the key world virtual is used to defined a function that will be overiden by the child contractfunctionactWise()publicviewvirtualreturns(stringmemory){return"Base is wise";}functionhello()publicviewreturns(stringmemory){return"Hello Motor";}}contractAisBaseContract{functionsayHello()publicviewreturns(stringmemory){//we are calling the hello function in the BaseContractreturnhello();}//we have overridden the base contract actWise function with our own implementationof itfunctionactWise()publicviewvirtualoverridereturns(stringmemory){return"Contract A is wise";}}contractBisBaseContract{//we have overriden the base contract actWise function with our own implementationof itfunctionactWise()publicviewvirtualoverridereturns(stringmemory){return"Contract B is wise";}}contractDisA,B,Bonny{functionactWise()publicviewoverride(A,B)returns(stringmemory){//super refers to the parent of the derived contract//if the inheritance is multiple super refers to the right most parent. In this case super refers to Breturnsuper.actWise();}functiongethelp()publicviewreturns(stringmemory){return"help me please!";}}
Enter fullscreen modeExit fullscreen mode

The example above consist of a base contract named BaseContract. TheBaseContract defines two methods namedactWise andsayHello.

The functionactWise is defined as virtual which means contracts inheriting (derived contracts) it, has an option to declare and execute their own implementation of the functionactWise.

This is seen by functionA andB which inherits from theBaseContract and implemented their own versions ofactWise by using theoverride keyword.

If you look closely at functionactWise in both contractA andB,you will notice that it still contain the virtual keyword. We made it so, so that any contract inheriting from them also have the option of overriding theactWise function.

Looking at contractD you can see that it inherits from both contractA and contractB. There is also a third inheritance from an interface calledBonny.

Before, I progress. What is an interface?
I answer my own question by defining an interface as a set of functions that a contract inheriting it must define and implement.

An interface only defines the function name and signature without implementation. The inheriting contract will not compile if the function defined on the interface is not implemented.

An interface is defined using theinterface keyword followed by the name of the interface. The functions defined in an interface must be external without a function body.

An interface cannot have a constructor and it does not make use of any state variable.

Coming back to earth

contractDisA,B,Bonny{functionactWise()publicviewoverride(A,B)returns(stringmemory){//super refers to the parent of the derived contract//if the inheritance is multiple super refers to the right most parent. In this case super refers to Breturnsuper.actWise();}functiongethelp()publicviewreturns(stringmemory){return"help me please!";}}
Enter fullscreen modeExit fullscreen mode

Weactwise function was overridden in both parent contractA andB (remember, we also defined theactWise function in contract A and B as virtual which means we can override it).

In our implementation, we decided to call the parent contractactWise function using the keywordsuper which means the parent contract. Since contract D has two parents that both have theactWise function; the parent at the most right hand side of the inheritance which is contractB will be called by super.

This example shows that the order of inheritance is important when you have more than one parent contract you are inheriting from.

Take away from the lesson:

  • Solidity supports multiple inheritance
  • Order of inheritance matter
  • An interface is a set of functions that a derived contract must implement
  • Inheritance is used to add extra functionality to our contracts.

Until next time, happy coding....

Top comments(0)

Subscribe
pic
Create template

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

Dismiss

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