Method in any programming language are those which reduce the burden of main method.Many of us got a problem where we can’t define two methods with the same name.Today we shall find a solution and write a sample program to explain this.
The main reason for this problem is,main method is not able to differentiate the methods with same name and same parameters.According to our present knowledge we used to change the name of method we shall compile the program.But that is not a solution to our problem.First we shall know how a main method differentiate the methods present in the program.It differentiate the methods depending upon the number of parameters and type of the parameters passed to the method.Therefore we can change the number of parameters or the type of parameters passed to the method.
Now we shall write program a with out method overloading and then use our above stated technique to solve the problem.
class methodover { int x,y; void getdata(int a) //Method which takes one parameter as input { x=a; y=0; } void getdata(int a) //Method which takes two parameteres as inputs { x=a; y=b; } void display() { System.out.println("Given value of the x is:"+x); System.out.println("Given value of the y is:"+y); } } class methodoverdemo { public static void main(String arg[]) { methodover ob1=new methodover(); methodover ob2=new methodover(); ob1.getdata(10); //Invokes the method which takes only one parameter. ob2.getdata(20,30); //Invokes the method which takes two parameters. System.out.println("Values of the variables assigned through first method"); ob1.display(); System.out.println("Values of the variables assigned through second method"); ob2.display(); } }
The output of the above program is
These are the errors which we got during compilation of the above program.
Now we shall write a program which solves these errors and compiles without errors using method overloading technique.
class methodover { int x,y; void getdata(int a) //Method which takes one parameter as input { x=a; y=0; } void getdata(int a,int b) //Method which takes two parameteres as inputs { x=a; y=b; } void display() { System.out.println("Given value of the x is:"+x); System.out.println("Given value of the y is:"+y); } } class methodoverdemo { public static void main(String arg[]) { methodover ob1=new methodover(); methodover ob2=new methodover(); ob1.getdata(10); //Invokes the method which takes only one parameter. ob2.getdata(20,30); //Invokes the method which takes two parameters. System.out.println("Values of the variables assigned through first method"); ob1.display(); System.out.println("Values of the variables assigned through second method"); ob2.display(); } }
The output of the above program is
The output of the above program is:
In our source code we changed the number of parameters into 2 such that there will be some difference between the methods written in the class.Thus the method overloading concept is applied to solve the problem.
You can download the source code.
Pingback: Constructor Overloading | letusprogram….!!!