Method
Overloading is the common way of implementing polymorphism. It is the
ability to redefine a function in more than one form. A user can
implement function overloading by defining two or more functions in a
class sharing the same name. Scala can distinguish the methods with
different method signatures. i.e. the methods can have the same name but
with different parameter list (i.e. the number of the parameters, the
order of the parameters, and data types of the parameters) within the
same class.
- Overloaded methods are differentiated based on the number and type of the parameters passed as an argument to the methods.
- We can not define more than one method with the same name, Order and the type of the arguments. It would be a compiler error.
- The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return type. It will throw a compile-time error.
Why do we need Method Overloading?
If we need to do the same kind of operation in different ways i.e. for different inputs. In the example described below, we are doing the addition operation for different inputs. It is hard to find many different meaningful names for single action.
If we need to do the same kind of operation in different ways i.e. for different inputs. In the example described below, we are doing the addition operation for different inputs. It is hard to find many different meaningful names for single action.
Method Overloading can be done by the:-
- The number of parameters in two methods.
- The data types of the parameters of methods.
- The Order of the parameters of methods.
By changing the number of parameters.
class Demo
{
def fun(p:Int, q:Int)
{
var Sum = p + q;
println("Sum in function 1 is:" + Sum);
}
def fun(p:Int, q:Int, r:Int)
{
var Sum = p + q + r;
println("Sum in function 2 is:" + Sum);
}
}
object Main
{
def main(args: Array[String])
{
var obj = new Demo();
obj.fun(6, 8);
obj.fun(5, 10, 58);
}
}
OutPut:-
Sum in function 1 is:14
Sum in function 2 is:73
By changing the Data types of the parameters
class Demo
{
def fun(p:Int, q:Int, r:Int)
{
var Sum = p + q + r;
println("Sum in function 1 is:"+Sum);
}
def fun(p:Double, q:Double, r:Double)
{
var Sum = p + q + r;
println("Sum in function 2 is:"+Sum);
}
}
object Main
{
def main(args: Array[String])
{
var obj = new Demo();
obj.fun(6, 8, 10);
obj.fun(5.9, 10.01, 58.7);
}
}
Output:
Sum in function 1 is:24
Sum in function 2 is:74.61
By changing the Order of the parameters
class Demo
{
def fun(name:String, No:Int)
{
println("Name of the watch company is:" + name);
println("Total number of watch :" + No);
}
def fun(No:Int, name:String )
{
println("Name of the watch company is:" + name);
println("Total number of watch :" + No);
}
}
object Main
{
def main(args: Array[String])
{
var obj = new Demo();
obj.fun("Rolex", 10);
obj.fun("Omega", 10);
}
}
Output:
Name of the watch company is:Rolex
Total number of watch :10
Name of the watch company is:Omega
Total number of watch :10
What happens when method signature is same and the return type is different?
The
compiler will give error as the return value alone is not sufficient
for the compiler to figure out which function it has to call. Only if
both methods have different parameter types (so, they have the different
signature), then Method overloading is possible.
object Main {
def main(args: Array[String]) {
println("Sum in function 1 is:" + fun(6, 8) );
println("Sum in function 2 is:" + fun(6, 8) );
}
def fun(p:Int, q:Int) : Int = {
var Sum: Int = p + q;
return Sum;
}
def fun(p:Int, q:Int) : Double = {
var Sum: Double = p + q + 3.7;
return Sum;
}
}
No comments:
Post a Comment