Solidity Polymorphism

In this article we will be having a look at solidity polymorphism techniques which will help us to use the solidity functions in an efficient way.

Solidity, just like any other programming language, has the capacity for processing data in multiple forms, which is known as polymorphism.

In particular, Solidity enables two kinds of polymorphism:

  1. Function Polymorphism.
  2. Contract Polymorphism.

Firstly, we will be learning about function polymorphism.



Solidity Polymorphism – Function

Method overloading, or Function Polymorphism, is a feature that enables declaring several functions with the same name within a contract or an inherited contract.

Two functions having the same name can be overloaded by changing the number of parameters or the data type of the parameters.

The following example shows the get_Sum() function being overloaded, by changing the data types of its parameters:

Example: 

pragma solidity ^0.8.0;contract SolidityTest{function get_Sum(uint mrx,uint ample) public pure returns(uint){return mrx+ample;}function get_Sum(int mrx,int ample) public pure returns(int){return mrx+ample; }}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Example Explanation

Above example is a Solidity smart contract that demonstrates function polymorphism, specifically method overloading.

The contract is called “SolidityTest” and defines two functions with the same name, “get_Sum“, but with different input parameter types (uint and int).

The first function takes two unsigned integers (uint mrx and uint ample) as inputs, and returns their sum as an unsigned integer (uint).

The second function takes two signed integers (int mrx and int ample) as inputs, and returns their sum as a signed integer (int).

This means that if a user calls the “get_Sum” function with two uint inputs, the first function will be executed and return a uint result.

On the other hand, if a user calls the same function with two int inputs, the second function will be executed and return an int result.

By utilizing function polymorphism in this way, the contract provides a more flexible and convenient interface for users, as they can call the same function name with different input types, without having to worry about the internal implementation details of the contract.

Correspondingly, the same process of function overloading can be done by changing the number of parameters of the function as shown below:

Example: 

pragma solidity ^0.8.0;contract SolidityTest{function get_Sum(uint mrx,uint ample) public pure returns(uint){return mrx+ample;}function get_Sum(uint mrx,uint ample,uint mrx_ample) public pure returns(uint){return mrx+ample+mrx_ample; }}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>
Note : If you find it challenging to execute the code above,do refer to our solidity first application article to get more understanding.

Lets move on to the second type of solidity polymorphism which is contract polymorphism.


Contract Polymorphism

It refers to the practice of utilizing multiple instances of related contracts interchangeably by means of inheritance.

This allows for child contract functions to be called using a parent contract instance.

The following example will help us to understand the contract polymorphism:

Example: 

pragma solidity ^0.8.0;contract SolidityTest{uint public mrx; uint public ample; function setValue(uint a,uint b) public { mrx=a; ample=b;}function getValue() public view returns(uint){ uint product=mrx*ample;return product;} }contract SolidityTest1{uint mrx1; uint ample1; uint mrx1_ample1; function setValue(uint a,uint b,uint c) public { mrx1=a; ample1=b; mrx1_ample1=c;}function getValue() public view returns(uint){ uint sum=mrx1+ample1+mrx1_ample1;return sum;} }contract SolidityTest2{SolidityTest obj1=new SolidityTest(); SolidityTest1 obj2=new SolidityTest1();function show_Both_Methods() public view returns(uint,uint){uint pro=obj1.getValue(); uint add=obj2.getValue(); return(pro,add);} }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Conclusion

Solidity polymorphism is considered as an efficient tool when it comes to any programming language.
This process not only helps us to use our functions efficiently but also helps us to use a few lines of code because of its technique of method overloading functions and contract overloading.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *