DEV Community

Ephraim Chukwu
Ephraim Chukwu

Posted on

SUPER KEYWORD IN SOLIDITY

Solidity, like other programming language, is a statically typed language for creating smart contracts with complex algorithmic programs. This higher language is afterwards compiled into opcodes that virtual machines understand. Despite the speciality of the language, it borrows ideas iimmensely from other existing programming languages. This article hopes to help you understand the super keyword in Solidity.

The Solidity language is designed with the attributes of inheritance and it supports polymorphism. While inheritance is simplying the sharing in the same functions as the inheritor contract, polymorphism is the ability to make internal or external calls to functions of the same name and the same type of parameters. Due to the possibilities of having a long hierarchy of inheritance, the super keyword was therefore introduced to enable easy accessibility.

The super keyword are mentioned often in contracts with a lot of inheritance possessing the some functions with the same parameter types. When this code is called, super.funcName(), it calls the last base contract before the contract that makes the call.

For instance, there are three contracts: A, B, C. If C inherits B and B inherits A, and they all have the same functions. If the super and the method function in the contract C, the last implementation of the function in B is what will be called. If A is the parent root contract, it won't be possible to use because it has no inheritance.

Here are practical examples:

pragma solidity 0.8.7;

contract A {
   function doSomething() external returns(bool) {}
}

contract B is A {
   function doSomething() external override returns(bool) {
     //super.doSomething();
}
}

contract C is B {
   function doSomething() external override returns(bool) {
   super.doSomething();
}
}
Enter fullscreen mode Exit fullscreen mode

The code block above indicates clearly what the super keyword do and how it is relevance in the tree of inheritance in solidity.

Top comments (0)