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

Supercharge with DappTools

DappTools: Supercharge Your Smart Contracts

DappTools is a suite of Ethereum focused CLI tools following the Unix design philosophy, favoring composability, configurability and extensibility. But…

Read Story

Smart Contract Security Roadmap

In 2021 $1.5 billion were hacked from smart contracts. That’s why smart contract security experts are in high demand and…

Read Story

Polygon Explained: How Polygon is Leading Scaling Solutions for Ethereum

Previously known as Matic Network, Polygon is a framework for building interconnected blockchain networks. It seeks to address some of…

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