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

Foundry Vs Hardhat

In this article, we will be comparing two smart contract development frameworks. Hardhat, which is the most used framework for…

Read Story

Rust Interview Questions

Is Rust Garbage Collected? No! One of Rust’s key innovations is guaranteeing memory safety without requiring garbage collection. How do…

Read Story
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

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