METHOD OVERLOADING IN JAVA

Method Overloading in Java





If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.


There are two ways to overload the method in java

  1. By changing number of arguments
  2. By changing the data type

Why Method Overloading is not possible by changing the return type of method only?

In java, method overloading is not possible by changing the return type of the method only because of ambiguity.

class Adder{  

static int add(int a,int b)

{return a+b;}  

static double add(int a,int b)

{return a+b;}  

}  

class Test{  

public static void main(String[] args){  

System.out.println(Adder.add(10,10));//ambiguity  

}}  


Can we overload java main() method?

Yes, by method overloading. You can have any number of main methods in a class by method overloading. But JVM calls main() method which receives string array as arguments only. 



Real life example of method overloading


We can save one contact with different mobile number .












Comments

Popular posts from this blog

OBJECT ORIENTED CONCEPTS

Arrays programms