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

How to do Upgradable smart contracts using OpenZeppelin

An Upgradable contract is a contract that can be (kind of) altered, after the deployment. At the time this article…

Read Story

Getting Started With Tenderly for Blockchain Development

Overview First things first, we need to get your Smart Contracts into Tenderly to use all of the timesaving features…

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

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