Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method Overloading" Watch "Method Overloading" New topic

Method Overloading

Savio Mascarenhas
Ranch Hand

Joined: Nov 29, 2000
Posts: 108
public class Test
public void method(Object o)
System.out.println("Object version");
public void method(String s)
System.out.println("String version");
public static void main(String args[])
Test test=new Test();
(a)Code does not compile.
(b)Code compiles cleanly & shows "Object version".
(c)Code compiles cleanly & shows "String version".
(d)Code throws a runtime exception.
The answer is (c).The value null can legally be assigned to both the overloaded methods then,how is it determined that the method taking a String argument should be invoked instead of the method that takes an Object argument ???
If the method taking an Object argument is replaced with any other object type(Integer,StringBuffer etc...)then,a compiler error "Reference to method is ambiguous" is displayed .

[This message has been edited by Savio Mascarenhas (edited December 08, 2000).]
Praveen Zala
Ranch Hand

Joined: Jul 02, 2000
Posts: 118
Though I am unable to answer ur question, I will make it easier
for others to ponder on this..
I have the following code fragment in my program :
String l[] = new String[1];
for(int i=0;i<1;i++)
Object ob[] = new Object[1];
for(int i=0;i<1;i++)
The output of this is :
This supports the fact that ARRAYS are AUTOMATICALLY initialized
in a default value......and since for strings and
object, it is "null" can anyone explain the constructor's
behaviour in Savio's code ?
Nice Qn Savio,
Praveen Zala
asim wagan
Ranch Hand

Joined: Nov 14, 2000
Posts: 62
<html><body>Please search through the archives it is present there.
It happens because java searches for the most specific class that can support a type of undefined object.Try the code elow and see it:
class a{
public void method(){
System.out.println("a version");
class b extends a{
public void method(){
System.out.println("b version");
public class c{
public static void main(String arg[]){
a a1=new a();
a a2=new b();
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
The way JVM does is to use most specific methods.
public class Testing1 {
public void addOne(int i) {
System.out.println("int version");
public void addOne(long l) {
System.out.println("long version");

public static void main(String args[]) {
Testing1 t = new Testing1();
The output is int version.
Even the call t.addOne(1), the argument 1 can be converted to long.
Aparna Narayanan
Ranch Hand

Joined: Nov 07, 2000
Posts: 44
Hi Savio,
Actually, as Adrian said, it searches for the most specific match. But, I guess, some more explanation is required. It checks whether the actual argument passed can be substituted for the lower most class in the hierarchy of formal parameter classes. If yes, then it substitutes.
In your code, String is the lower most in the hierarchy of classes(Object and String) used in the overloaded methods. So, it substitutes for String and you get the output as "String Version"
whereas, when you use Integer, Byte, Short, String which are all in the same level of hierarchy, there's no question of which is the "single" lowermost in the hierarchy and hence it reports of an ambiguity in using one of the overloaded methods.
Hope I am clear.
I agree. Here's the link:
subject: Method Overloading
It's not a secret anymore!