fbpx

A Guide to Inheritance in Solidity

Rahul Ravindran

Inheritance allows code to be reused by creating a new class from an existing class. The new class that is created is known as subclass (child or derived class) and the existing class from where the child class is derived is known as superclass (parent or base class).

The is keyword is used to perform inheritance Solidity

contract Animal {
  // methods and fields
}

contract Cat is Animal {

  // methods and fields of Animal
  // methods and fields of Cat
}

In the above example, the Cat contract is created by inheriting the methods and fields from the Animal contract.


A derived contract can get to all non-private internal methods and states.

Function superseding

You can call parent contract’s function from the client contract using function superseding. This can be done using the super keyword.

contract Animal {
  // methods and fields
  function run(){
  ...
  }
}

contract Cat is Animal {

  // methods and fields of Animal
  // methods and fields of Cat

  function hunt(){
  //function superseding
   super.run()
  }
}

The super keyword in Solidity gives access to the immediate parent contract from which the current contract is derived. When having a contract A with a function f() that derives from B which also has a function f(), A overrides the f of B. That means that myInstanceOfA.f() will call the version of f that is implemented inside A itself, the original version implemented inside B is not visible anymore. The original function f from B (being A‘s parent) is thus available inside A via super.f(). Alternatively, one can explicitly specifying the parent of which one wants to call the overridden function because multiple overriding steps are possible as exemplified in the example below:

pragma solidity ^0.4.5;

contract C {
  uint u;
  function f() {
    u = 1;
  }
}

contract B is C {
  function f() {
    u = 2;
  }
}

contract A is B {
  function f() {  // will set u to 3
    u = 3;
  }
  function f1() { // will set u to 2
    super.f();
  }
  function f2() { // will set u to 2
    B.f();
  }
  function f3() { // will set u to 1
    C.f();
  }
}

Reference:
– https://ethereum.stackexchange.com/questions/12920/what-does-the-keyword-super-in-solidity-do
– https://www.educative.io/edpresso/what-is-inheritance-in-solidity

0 Comments

Leave a Reply

More great articles

Here Is Exactly How To Get Your First Web3 Developer Job: A Quick Guide To Getting Started In Crypto as a Developer in 2023

A lot of people have been wanting to get into web3 and land their first web3 job. Web3 is an…

Read Story

How to sign messages using Metamask

Signing messages in Ethereum Authentication for web3 ? If you’ve ever interacted with an Ethereum Dapp you’ve probably been asked…

Read Story

Solidity Gas Optimization Tip

Solidity is a special language with many little quirks. A lot of things behave differently in Solidity than most other…

Read Story

Never miss a minute

Get great content to your inbox every week. No spam.
[contact-form-7 id="6" title="Footer CTA Subscribe Form"]
Arrow-up