Menu
Topics Index
...
`

Method Overloading235


If you need explanation Read this topic

If you need Answer Take test on this topic

Method Overloading235
What will be the output of the following program?
class MethodOverloading
{
    public static void main(String s[])
    {
        print();
        print(8);
        int j = print(9);
    }

    public static void print()
    {
        System.out.println("Called print with no parameters");
    }

    public static void print(int i)
    {
        System.out.println("Called print with int parameter with return type void");
    }

    public static int print(int i)
    {
        System.out.println("Called print with int parameter with return type int");
    }
}

A. Called print with no parameters
Called print with int parameter with return type void
Called print with int parameter with return type int
B. Compilation Error - since there are two methods with int parameter but return type is different.
C. Compilation Error - since there are three methods with the same name print.
D. Runtime Error - It fails with a runtime error, since it does not know which print method to call.
Topic: Method Overloading In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is  B...here we have the concept of the method overloading...method overloading means same method name with different type of,order of,number of arguments...in the method overloading return type has no significance...
int fum();
int fun(int a)---method overloading

int fun();
void fun();--here compilation error Bcz return dont have any significance...

here we have the method name print()
here we have int print(int i),void print(int i) so here compiler will raise an error saying bcz two methods should not same...so here compiler will raise..

Posted by Uday Kumar    2014-12-19 11:14:22


Answer is B...
A class can have multiple methods with same name but the number of arguments,type of arguments or the order of arguments has to differ from one method to the other method,then it is known as Method Overloading.
Generally,method overloading improves the readability of the program.
consider the following examples
a) void mul(int a,int b)  {System.out.println(a+b);}  
  void mul(int a,int b,int c) {System.out.println(a+b+c);}  
  THe above example is a valid scenario of method overloading as the number of arguments differs

b) void mul(int a,int b){System.out.println(a+b);}  
  void mul(double a,double b){System.out.println(a+b);}  
  THe above example is also  a valid scenario of method overloading as the type of arguments differs

  Similarly type of arguments can also be varied

But, Method Overloading is not possible with the below scenario

   int mul(int a,int b){System.out.println(a+b);}
  double mul(int a,int b){System.out.println(a+b);}

  In the above case,JVM cannot resolve the call to the method obj.mul(a,b)....Because of this ambiguity it throws a compiler error..
  Since in the program,we have the following method declarations
  public  static void print(int i){}
  public static int print (int i) {}
  JVM can resolve the method call of print() but it cannot resolve print(8) as there are two methods with same arguments,hence it throws a compiler error

Posted by Shashanka Mogaliraju    2014-12-20 07:49:14


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Shashanka Mogaliraju,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Asad Ahmed. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-22 06:58:35


© meritcampus 2019

All Rights Reserved.

Open In App