Menu
Topics Index
...
`

Method Overloading275


If you need explanation Read this topic

If you need Answer Take test on this topic

Method Overloading275
What will be the output of the following program?
class MethodOverloading
{
    public static void main(String s[])
    {
        A a = new A();
        int x = a.method1(4, 5); // LINE X
    }
}

class A
{
    void method1(int a)
    {
        System.out.println("method1 with a called");
    }

    void method1(int a, int b)
    {
        System.out.println("method1 with a and b called");
    }

    int method1(int a, int b) // LINE Y
    {
        System.out.println("method1 which returns int is called");
    }
}

A. method1 with a and b called
B. method1 which returns int is called
C. Compilation Error at LINE Y - since we can not have two methods 
with same method name and parameters
D. Runtime Error at LINE X - since it does not know which method1 should be called.
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 c.if 2 or more method in a class hav same name but diff parameter,it is known as Method overloading .

method overloading is one of the ways through which java supports polymorphism.method ovrloading can be done by changing num of args or by changing the data type of args.if 2 or more method hav same name and same parameter list but differs in return type are not said to be overloaded method.coming to this program method1 with same num of args and diff return type void and int is not an overloaded method.upto void method1 compilr successfully compiled after it encounters same method name with same arg but with diff return type,compilr wont compile this method  bcz this not coming under oops concept....so line Y throws error

Posted by Maheshwari Natarajan    2014-12-23 04:35:36


This dose is now closed and the winners are Sai Ram,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is PaWan Mulchandani. 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-23 04:51:21


© meritcampus 2019

All Rights Reserved.

Open In App