DEV Community

Cover image for Function Overloading and Overriding in Solidity
Shlok Kumar
Shlok Kumar

Posted on

Function Overloading and Overriding in Solidity

Overloading allows multiple definitions for the same function name, while Overriding allows a subclass or child class to provide a specific implementation.

Overloading

You can have numerous definitions for the same function name in the same scope if the function name is different. The types of arguments in the argument list, as well as the number of arguments in the argument list, must be different for each definition of the function. It is not possible to overload function declarations that differ simply in the type of return they return. 

Function overloading in Solidity allows multiple definitions for the same function name within the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. We cannot overload function declarations that differ only by return type. The concept of function overloading in Solidity can be illustrated with an example. For instance, we can define two functions with the same name "getSum" but different parameters: one takes two arguments and returns their sum, while another takes three arguments and returns their sum.

Function overriding is another feature of object-oriented programming that allows an inheriting contract to override its performance. A virtual function permits an inheriting contract to override its performance, while a function that overrides the base function should be distinct as an override.

Function overloading is usually implemented using static polymorphism, where a function call is resolved using some "best match" algorithm, where the particular function to call is resolved by finding the best match of formal parameter types with actual parameter types.

Example:

pragma solidity ^0.8.0;

contract Test {
   function getSum(uint x, uint y) public pure returns(uint){      
      return a + b;
   }
   function getSum(uint x, uint y, uint z) public pure returns(uint){      
      return a + b + c;
   }
   function callSumWithTwoArguments() public pure returns(uint){
      return getSum(1,2);
   }
   function callSumWithThreeArguments() public pure returns(uint){
      return getSum(1,2,3);
   }
} 
Enter fullscreen mode Exit fullscreen mode

Overriding

A virtual function is one that allows an inherited contract to override the operation of a function that is defined as such. The function that overrides the base function should be labeled as an override in Solidity in order to distinguish it from the base function.

In Solidity, if a function with the same name and signature as a function in the parent contract is declared in the child contract, it will override the parent function. The override specifier must be used to indicate that a function is intended to override another function. If an overridden function is called from within the overriding function, it can be done using super.functionName(arguments).

To override a method of a superclass in IntelliJ IDEA, you can generate necessary code from a predefined template. IntelliJ IDEA creates a stub that contains a call to the method of the superclass, leaving you with the task of providing some meaningful source code in the method's body.

In Python, an overriding method is best implemented by delegating some of the work to the same method in the superclass. This is known as the Template-Method design pattern. Often, an overridden method needs to call its superclass's version as part of its own operation. This can be done by explicitly getting the method as a class attribute and passing an instance as its first argument.

Example:

pragma solidity ^0.8.0;

contract Base1
{
   function foo() virtual public {}
}

contract Base2
{
   function foo() virtual public {}
}

contract Inherited is Base1, Base2
{
   // Derives from multiple bases defining foo(), so we must explicitly
   // override it
   function foo() public override(Base1, Base2) {}
Enter fullscreen mode Exit fullscreen mode

For more content, follow me at - https://linktr.ee/shlokkumar2303

Top comments (0)