• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading Puzzled

 
VIGNESHWAR VISWANATHAN
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's Good Manfred, i must have done like that.


Thanks

Anil Kumar
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello people,

Please have a look at this thread.

HtH.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic