Interface and Inheritance in Solidity

Esther BreathEsther Breath
4 min read

In this article, we shall be looking into the above two interesting concepts concerning Solidity.

Interface and inheritance help to achieve code reusability and structured design.

Both concepts can be used interchangeably, but they serve distinct purposes.

In this article, we shall be exploring these concepts and explore how they contribute to the Solidity development paradigm.

What is Interface in Solidity?

Interface in solidity is similar to abstract contract. An abstract contract is a contract that cannot be instantiated on its own but is intended to serve as a base for other contracts. It must contain one or more functions without providing their implementation details.

Abstract contracts are used as templates or blueprints for other contracts that will inherit from them.

Abstract contracts are similar to interfaces in that they define a set of functions that must be implemented by derived contracts. However, unlike interfaces, abstract contracts can also include state variables and function implementations.

As mentioned earlier, the interface is used as a template or blueprint for another contract, but can not have any function with implementation.

Interfaces are often used to enforce a standard among multiple contracts.

Interfaces can be useful if you want to make a call from a different contract using the keyword “interface”. The calling contract will have access to the signature of the method calls which is provided by the interface.

Any contract that claims to implement an interface must provide implementations for all the functions declared in the interface. This enforces a consistent API across contracts and ensures compatibility

The following are some of the characteristics of interfaces

  • Functions of an interface can be only of type external.

  • The interface can not have a constructor.

  • The interface can not have state variables.

  • The interface can have an enum, structs which can be accessed using interface name dot notation

    Below is an example;

What is an Inheritance?

A contract can inherit traits and actions (state variables and functions) from another contract, known as the parent or base contract, through inheritance. The inherited functionality may be increased, replaced, or added to by the inheriting contract, also referred to as the child or derived contract.

Inheritance involves actual code sharing. The child contract inherits the state variables and functions of the parent contract unlike implementing declared codes as in the case of interface.

Multiple inheritance for contracts is also supported by Solidity. Multiple parent contracts may be inherited by a child contract, allowing for intricate contract arrangements.

We cannot talk about inheritance in solidity without mentioning is keyword.

in Solidity the is keyword is used to denote contract inheritance. It signifies that one contract is inheriting from another, which allows the inheriting contract to access the state variables and functions of the parent contract.

Below is how is keyword is used in contract inheritance.

The above example demonstrates a 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.

When using solidity developers can change how a function in the parent contract is implemented in the derived contract. 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 class. The overriding function needs to have the keyword override. 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.

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 is to prevent function overloading.

modifiers can be overridden in the derived contract just as in functions, only if they were marked with the keyword virtual in the parent class. You have to use the override keyword while overriding a modifier in the derived class.

Conclusion

In conclusion, both interfaces and inheritance play important roles in creating effective and maintainable smart contracts, depending on what the developer wants to achieve. Remember to use the interface keyword when using an interface and to use the is keyword for inheritance. Don’t forget to use the virtual and override keywords when implementing function overriding in your applications.

0
Subscribe to my newsletter

Read articles from Esther Breath directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Esther Breath
Esther Breath