• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overloading Puzzled

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's Good Manfred, i must have done like that.


Thanks

Anil Kumar
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello people,

Please have a look at this thread.

HtH.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic