sponsor

sponsor

Slider

Recent Tube

Business

Technology

Life & style

Games

Sports

Fashion

» » Method Overloading

Method Overloading में एक class पर एक ही नाम के multiple method हो सकते है | लेकिन उन methods की arguments की संख्या और उनका type अलग-अलग होता है |
Program पर Constructor Overloading भी किया जाता है |
Method Overloading का return-type से कोई लेना-देना नहीं होता |
For eg. अगर program में एक class के अन्दर same signature के दो methods होते है तो Ambiguity Error आ जाता है , चाहे उनका return-type अलग हो या ना हो | इसका मतलब ये है कि, return-type से Method Overloading का कोई सम्बन्ध नहीं है |
Source Code :
class Sample{
 
 int disp(int x){
  return x; 
 }
 double disp(int y){
  return y;
 }
 public static void main(String args[]){

 Sample s = new Sample();
 System.out.println("Value of x : " + s.disp(5));
 System.out.println("Value of y : " + s.disp(6.5));
 }  
}
Output :
Sample.java:6: error: method disp(int) is already defined in class Sample

Method को दो प्रकार से overload किया जाता है |

  1. Changing types of Arguments/Parameters
  2. Changing Number of Arguments/Parameters

1. Changing types of Arguments/Parameters

Program में तीन same name के methods लिए है | लेकिन उनके argument का type अलग-अलग लिया है | return-type इसीलिए change नहीं किया चूँकि lossy conversion का error ना आ जाए |
Source Code :
class Sample{
 
 int disp(int x){
  return x;
 }
 float disp(float x){
  return x;
 }
 double disp(double x){
  return x;
 }
 public static void main(String args[]){

 Sample s = new Sample();
 System.out.println("Value of x : " + s.disp(5));
 System.out.println("Value of x : " + s.disp(6.5));
 System.out.println("Value of x : " + s.disp(7.54));
 }  
}
Output :
Value of x : 5
Value of x : 6.5
Value of x : 7.54

Lossy Conversion Occur using Same return-type


Program में तीन same methods लिए है लेकिन उनके return-type एक जैसे है | अगर method पर int type का argument pass किया गया है और उसको int type में ही return किया गया है, इससे कोई lossy conversion की error नहीं आती चूँकि int to int conversion हो रहा है |
Method पर float type का argument लिया जाता है और उसे int type में return किया जाता है, तो यहाँ पर lossy conversion की संभावना होती है |
Method पर float type को int type में convert नहीं किया जा सकता |
Source Code :
class Sample{
 
 int disp(int x){
  return x;
 }
 int disp(float x){
  return x;
 }
 int disp(double x){
  return x;
 }
 public static void main(String args[]){

 Sample s = new Sample();
 System.out.println("Value of x : " + s.disp(5));
 System.out.println("Value of x : " + s.disp(6.5));
 System.out.println("Value of x : " + s.disp(7.54));
 }  
}
Output :
Sample.java:7: error: incompatible types: possible lossy conversion from float to int
                return x;
                       ^
Sample.java:10: error: incompatible types: possible lossy conversion from double to int
                return x;

Type Conversion


Program पर data type का convrsion किया गया है |
पहले disp() method पर int to float में convert किया गया है और दुसरे disp() पर float to double convert किया गया है |
Source Code :
class Sample{
 
 float disp(int x){
  return x;
 }
 double disp(float x){
  return x;
 }
 public static void main(String args[]){

 Sample s = new Sample();
 System.out.println("Value of x : " + s.disp(5));
 System.out.println("Value of x : " + s.disp(6));
 }  
}
Output :
Value of x : 5.0
Value of x : 6.0

2. Changing Number of Arguments/Parameters

Arguments/Parameters की संख्या बदलकर भी एक class के अन्दर same methods का इस्तेमाल किया जा सकता है |
Source Code :
class Sample{
 
 int disp(int x){
  return x;
 }
 int disp(int x, int y){
  return x + y;
 }
 int disp(int x, int y, int z){
  return x + y + z;
 }
 public static void main(String args[]){

 Sample s = new Sample();
 System.out.println("Value of x: " + s.disp(5));
 System.out.println("Addition of x and y : " + s.disp(5, 9));
 System.out.println("Addition of x, y and z : " + s.disp(5, 9, 5));
 }  
}
Output :
Value of x: 5
Addition of x and y : 14
Addition of x, y and z : 19

Example for Valid Sequence of Arguments types

disp(char, int)
disp(int, char)

Source Code :
class Sample1
{
   void disp(char c, int x)
   {
       System.out.println("Value of c : " + c);
    System.out.println("Value of x : " + x);
   }
   void disp(int x, char c)
   {
       System.out.println("Value of x : " + x);
    System.out.println("Value of c : " + c);
   }

   public static void main(String args[])
   {
       Sample1 s = new Sample1();
       s.disp('c', 12);
       s.disp(13, 'h');
   }
}
Output :
Value of c : c
Value of x : 12
Value of x : 13
Value of c : h

Example for invalid Sequence of Arguments types

disp(float, int)
disp(int, float)

Source Code :
class Sample
{
   void disp(float x, int y)
   {
       System.out.println("Value of x : " + x);
    System.out.println("Value of y : " + y);
   }
   void disp(int x, float y)
   {
       System.out.println("Value of x : " + x);
    System.out.println("Value of y : " + y);
   }

   public static void main(String args[])
   {
       Sample s = new Sample();
       s.disp(10, 14);
       s.disp(2, 3);
   }
}
Output :
  both method disp(float,int) in Sample and method disp(int,float) in Sample match
Sample.java:18: error: reference to disp is ambiguous
       obj.disp(2, 3);
          ^
  both method disp(float,int) in Sample and method disp(int,float) in Sample match

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply