LanguagesGuide to Inheritance in Solidity

Guide to Inheritance in Solidity

Solidity Blockchain programming tutorial

Inheritance in the Solidity programming language allows a programmer to extend a contractor’s attributes and properties to their derived contracts. Developers can also modify these aspects in the derived contract as well via a process known as overriding.

Unlike other programming languages like Java, Solidity allows for multiple inheritances. Multiple inheritance refers to the ability of a derived contract to have more than one parent contract. This, therefore, means that a single contract can inherit from multiple contracts at the same time.

In this Solidity software development tutorial, programmers will learn how to achieve inheritance in their Solidity code. Code examples will be provided to make the learning process easier.

Read: The Best Programming Languages to Learn

Using the “is” Keyword in Solidity

To create a derived (or inheriting) contract, simply use the is keyword, as demonstrated in the example code below:

# A is a derived contract of B
contract A is B{


As mentioned earlier, Solidity allows for multiple inheritances. You can implement multiple inheritances in solidity as shown in this sample code:

contract A{


# single inheritance 
contract B is A{


#multiple inheritance 
contract C is A,B {


The implementation above has been carefully chosen to demonstrate a particularly interesting case of multiple inheritances in Solidity. Take note that one of the contracts that C is deriving from is also a derived contract. That is, contract B is also derived from contract A.

This is not an error – Solidity allows this type of multiple inheritances as well, and your code should compile without any errors.

Read: Python versus Java Programming Language Comparison

Function Overriding in Solidity

Solidity lets developers change how a function in the parent contract is implemented in the derived class. This is known as function overriding.

The function in the parent contract needs to be declared with the keyword virtual to indicate that it can be overridden in the deriving contract.

In addition, the overriding function needs to have the keyword override. It is possible that you may want your overriding function to be overridden by another function. This is acceptable and can be achieved by using the virtual keyword as before. Here is an example demonstrating function overriding in Solidity:

contract A {
function play(int x, int y) public virtual {


contract A  is B{
function play(int a, int b) public override {


In the derived contract above, the overriding function needs to have the same function name and the same number of arguments and types as in the parent class.

This may seem like something trivial to point out. However, it’ i possible to have a scenario where the parent contract has multiple methods with the same name and different parameter lists. This is a scenario known as function overloading.

With this in mind, it is important for programmers to put into consideration the parameter list of their functions. Otherwise, you may end up overriding the wrong function in the event of function overloading in the parent contract.

Read: Manage Visibility of Variables and Functions in Solidity

Modifier Overriding in Solidity

Modifiers are properties that are used to change the behavior of functions. Just like functions, modifiers can be overridden in the derived contract only if they were marked with the keyword virtual in the parent class.

Similarly, you have to use the override keyword while overriding a modifier in the derived class. Check out the sample implementation below, which demonstrates modifier overriding in Solidity:

contract A{
modifier X virtual {

contract B is A{
modifier X override {

Final Thoughts on Inheritance in Solidity

Solidity allows software developers to inherit functions, state variables, and function modifiers. Additionally, it allows for instances of multiple inheritance. Solidity also supports polymorphism through function overriding. Remember to use the keywords virtual and override when implementing function overriding in your applications.

Read: Top Online Courses to Learn Python

Latest Posts

Related Stories