Polymorphism
Polymorphism means having multiple forms. There are the following two types of polymorphism:
- Function polymorphism
- Contract polymorphism
Function polymorphism
Function polymorphism refers to declaring multiple functions within the same contract or inheriting contracts having the same name. The functions differ in the parameter data types or in the number of parameters. Return types are not taken into consideration for determining valid function signatures for polymorphism. This is also known as method overloading.
The next code segment illustrates a contract that contains two functions, which have the same name but different data types for incoming parameters. The first function, getVariableData
, accepts int8
as its parameter data type, while the next function having the same name accepts int16
as its parameter data type. It is absolutely legal to have the same function name with a different number of parameters of different data types for incoming parameters as shown in the following...