wellnwill Marathahalli Bangalore

Method overloading in java


Method overloading in java is also known as compile time polymorphism, early binding, static polymorphism. Creating multiple methods with same name but different parameters (arguments) is known as method overloading.

Parameter ((arguments) must be differ in terms of

  • Number of arguments
  • Type of arguments
  • Order of arguments

Note- Method overloading is not possible by changing the return type of methods.

Purpose of Method overloading: To improve the readability of program.


Achieving method overloading by changing the number of parameters


class Bulb 
{  
  int watt;  
  String color;
  float price; 
  String size;
  
  void insertValue(int w, String c, float p)
  {   
   watt=w;  
   color=c;  
   price=p;
   System.out.println("data inserted with default size");
  }  
  
void insertValue(int w, String c, float p, String s) 
  {   
   watt=w;  
   color=c;  
   price=p;
   size=s;
   System.out.println("data inserted with specific size");
  }

void showValue()
  {
  System.out.println("display data");  
  System.out.println("watt :"+ watt);
  System.out.println("color :"+ color);
  System.out.println("price :"+ price);
  System.out.println("size :"+ size);
  } 
  public static void main(String args[]){  
  Bulb b1=new Bulb();  
  Bulb b2=new Bulb(); 
  b1.insertValue(100,"white",15.50F);  
  b2.insertValue(200,"blue",25.75F,"small");  
  
  b1.showValue(); 
  System.out.println("---------");  
  b2.showValue();
   }  
}





	   

Achieving method overloading by changing the type of parameters


class Bulb
 {  
  int watt;  
  String color;
  double price; 
  
void insertValue(int w, String c, float p)
  {   
   watt=w;  
   color=c;  
   price=p;
   System.out.println("data inserted with float price");
  } 
  
void insertValue(int w, String c, double d)
  {   
   watt=w;  
   color=c;  
   price=d;
   System.out.println("data inserted with double price");
  }


void showValue()
  {
  System.out.println("display data");  
  System.out.println("watt :"+ watt);
  System.out.println("color :"+ color);
  System.out.println("price :"+ price);
  } 
  
 public static void main(String args[])
 {  
  Bulb b1=new Bulb();  
  Bulb b2=new Bulb(); 
  
  b1.insertValue(100,"white",15.50F);  
  b2.insertValue(200,"blue",25.75D);  
  
  b1.showValue(); 
  System.out.println("---------");  
  b2.showValue();
  
 }  
}

	   

Achieving method overloading by changing the order of parameters


class Bulb{  
 int watt;  
 String color;
 float price; 
  
 void createBulb(int w, String c, float p)
 {   
  watt=w;  
  color=c;  
  price=p;
  System.out.println("parameter order: int, string, float");
 }  
 
  void createBulb(int w, float p, String c)
 {   
  watt=w;  
  color=c;  
  price=p;
  System.out.println("parameter order: int, float, String");
 } 
}

class Test155
{  
 public static void main(String args[]){  
  Bulb b1=new Bulb();  
  Bulb b2=new Bulb(); 
  
  b1.createBulb(100,"white",15.50F);  
  b1.createBulb(200,25.75F,"blue");  
 }  
}  


Method overloading with Type promotion


class Bulb
 {  
  int watt;  
  String color;
  double price; 
 
 void insertValue(int w, String c, double d)
  {   
   watt=w;  
   color=c;  
   price=d;
   System.out.println("data inserted with double type price");
  } 
  void showValue()
  {
  System.out.println("display data");  
  System.out.println("watt :"+ watt);
  System.out.println("color :"+ color);
  System.out.println("price :"+ price);
  }
public static void main(String args[]){  
  Bulb b1=new Bulb();  
  b1.insertValue(100,"white",15.50F); 
//here 3rd  float literal will be promoted to double  
  b1.showValue(); 
  }  
}


If there are matching type arguments in the method, type promotion will not be performed


class Bulb
 {  
  int watt;  
  String color;
  double price; 
  
  void insertValue(int w, String c, float p)
  {   
   watt=w;  
   color=c;  
   price=p;
   System.out.println("data inserted with float price");
  }  
 
  void insertValue(int w, String c, double d)
  {   
   watt=w;  
   color=c;  
   price=d;
   System.out.println("data inserted with double price");
  } 
void showValue()
  {
  System.out.println("display data");  
  System.out.println("watt :"+ watt);
  System.out.println("color :"+ color);
  System.out.println("price :"+ price);
  } 
  
 public static void main(String args[]){  
  Bulb b1=new Bulb();  
  Bulb b2=new Bulb(); 
  
  b1.insertValue(100,"white",15.50F);  
  b1.showValue(); 
 }  
}





	   

Footer with Map