aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading Puzzled Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading Puzzled" Watch "Overloading Puzzled" New topic
Author

Overloading Puzzled

VIGNESHWAR VISWANATHAN
Greenhorn

Joined: May 26, 2007
Posts: 17
public class Confusing {

private Confusing(Object o) {

System.out.println("Object");

}



private Confusing(double[] dArray) {

System.out.println("double array");

}



public static void main(String[] args) {

new Confusing(null);

}

}
What is the output and explain why?
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

The output is double array

Because you have overloaded Confusing constructor with object as one and double array as one,Here dArray IS-A Object.So the compiler goes with most specific.

Try
like this

Confusing(String s){}
Confusing(StringBuffer s){}

This time you will get compile time error,because there is no relation between String and StringBuffer and the compiler does not have idea for which it has to go.


Thanks

Anil Kumar
VIGNESHWAR VISWANATHAN
Greenhorn

Joined: May 26, 2007
Posts: 17
Hi Anil.please be more elaborate. here dArray IS-A Object.What are you trying to justify.The default values of both Object and the array of double reference is null.Then how can you say that the output is "double array".
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

When ever the compiler finds that it can call more than one method,it will try to call for more specific one,in other words it will go for down in the inheritance tree

Object
|
|
|--int[]
|--double[]
|--float []

In your example you have given both Object and double array.Now the compiler know that with null it can call both methods,so it will go for down the hieraricy.If it finds that there is no relation ship between the references you gave in the overloading method,then the compiler will give compile time error.

Try with String and StringBuffer, which i mentioned in the above post

You will get a fair idea.


Thanks

Anil Kumar

[ June 20, 2007: Message edited by: anil kumar ]
[ June 20, 2007: Message edited by: anil kumar ]
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
Hi Anil !
one question for you.Your answer is correct .but i want to know than why below line is not compiling...
System.out.println(null);

Why this logic is not applicable here
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

raj
----------------------------------------------------------

void println(Object x)
Print an Object and then terminate the line.
void println(String x)
Print a String and then terminate the line.
void println(char[] x)
-------------------------------------------------------------

These are the methods from API

Now null can be applied to every one,but here char[] and String has no relation ship and the compiler knows this it will compile time error.

Please see the above post for hierarchy


Thanks

Anil Kumar
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Hi raj,

have a look at this Discussion.

The example is more complicated, but if you understand it, you shouldn't have any problems with method overloading.
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
Thanks Anil its clear now!Thanks Manfred for your post also .Its good one
[ June 20, 2007: Message edited by: raj malhotra ]
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
That's Good Manfred, i must have done like that.


Thanks

Anil Kumar
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hello people,

Please have a look at this thread.

HtH.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading Puzzled