Results 1 to 5 of 5
Thread: Method Overloading - Doubt
- 01-31-2011, 08:28 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 1
- Rep Power
- 0
Method Overloading - Doubt
Hi,
what is the output of the following code snippet and plese explain the reason for the output.
class Demo
{
void eat(Object o)
{
System.out.println("Object");
}
void eat(String a)
{
System.out.println("String");
}
public static void main(String ap[])
{
new Demo().eat(null);
}
}
- 01-31-2011, 08:38 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,377
- Blog Entries
- 7
- Rep Power
- 17
- 01-31-2011, 09:42 PM #3
You spent all the time typing that in, and you couldn't click two buttons to compile and run it yourself?
- 02-01-2011, 02:22 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Overloading is a simple concept, I don't want to give too much away, if you take some time and think it's easy to understand. If you want to have one method that takes multiple different types of arguments and still works, you can overload it to take a different arg list.
for example:
This allows you to supply the same method with different arguments and have it still work. If you did not overload it and try passing a String to the first method it will not work correctly since it requires an int.Java Code:public int square(int x){ return x * x; } public int square(String x){ int y = Integer.parseInt(x); return y * y; }
- 02-01-2011, 06:47 AM #5
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
Similar Threads
-
Method Overloading Verification
By lala in forum New To JavaReplies: 22Last Post: 11-30-2010, 05:32 AM -
Overloading qn
By diskhub in forum New To JavaReplies: 18Last Post: 06-07-2010, 05:19 PM -
method overloading and arrays.
By glopez09 in forum New To JavaReplies: 5Last Post: 12-08-2009, 03:02 PM -
Demonstration of both constructor and ordinary method overloading
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:43 PM -
Method/Operator Overloading
By Java Tip in forum Java TipReplies: 0Last Post: 12-01-2007, 08:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks