envoy1084
/
30-Days-of-Solidity
30 Days of Solidity step-by-step guide to learn Smart Contract Development.
WARNING: This repository is currently undergoing updates and revisions to incorporate the latest information and advancements in Solidity programming. Please be advised that the content may not be up-to-date or accurate during this time. We expect the updates to be completed within the next 30 days, and appreciate your patience during this process. Thank you for your understanding.
Contents
- Day 1 - Licenses and Pragma
- Day 2 - Comments
- Day 3 - Initializing Basic Contract
- Day 4 - Variables and Scopes
- Day 5 - Operators
- Day 6 - Types
- Day 7 - Functions
- Day 8 - Loops
- Day 9 - Decision Making
- Day 10 - Arrays
- Day 11 - Array Operations
- Day 12 - Enums
- Day 13 - Structs
- Day 14 - Mappings
- Day 15 - Units
- Day 16 - Require Statement
- Day 17 - Assert Statement
- Day 18 - Revert Statement
- Day 19 - Function Modifiers
- Day 20…
This is Day 30 of 30 in Solidity Series
Today I Learned About Polymorphism in Solidity.
Polymorphism is an ability to process data in more than one form. Like any other programming language Solidity also supports polymorphism. Solidity supports two types of polymorphism, Function Polymorphism, and Contract Polymorphism.
Function Polymorphism
Function Polymorphism is also known as method overloading. In function polymorphism, multiple functions are declared having the same name within the same contract or inheriting contract. Functions differ on the basis of the number of parameters or parameter datatypes. Declaration of function cannot be overload by functions that differ only in return type.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
// Contract definition
contract methodOverloading {
// Function to get value of the string variable
function getValue(string memory _strin) public pure returns(string memory) {
return _strin;
}
// function to get value of the unsigned integer variable
function getValue(uint _num) public pure returns(uint) {
return _num;
}
}
Contract Polymorphism
Contract polymorphism means using multiple contract instances interchangeably when they are related to each other by using inheritance. This helps in calling the child contract functions using the instance of the parent contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract parent{
// Internal state variable
uint internal sum;
// Function to set the value of internal state variable sum
function setValue(uint _num1, uint _num2) public {
sum = _num1 + _num2;
}
// Function to return a value 10
function getValue() virtual public view returns(uint) {
return 10;
}
}
// Defining child contract
contract child is parent{
// Function getValue overloaded to return internal state
// variable sum defined in the parent contract
function getValue() override public view returns(uint) {
return sum;
}
}
// Defining calling contract
contract ContractPolymorphism {
// Creating object
parent pc = new child();
// Function to set values of 2 unsigned integers
function getInput(uint _num1, uint _num2) public {
pc.setValue(_num1, _num2);
}
// Function to get value of internal state variable sum
function getSum() public view returns(uint) {
return pc.getValue();
}
}
When we deploy ContractPolymorphism contract and call function getInput with values 52 and 78 and then call the getSum method we get
0:
uint256: 130
Top comments (1)
Thanks for the Info! Was quite helpful. Your example code allowed me to understand the purpose of Contract Polymorphism, which was quite nebulous in my head!