• 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

Which one is come 1'st ?

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Good morning to all.In my class has a 2 constructor with one argument,in which one constructor argument is object and then another constructor argument is string.At this synario i create a instace for that class pass null argument.Which one is excute.
I'm getting the answer is String argument.
Why the Object argument doesn't excute the first, cause Object all for the base class.
What is reason
Even as i wrote this sample code as well
-----------------
class sample
{
sample(Object o)
{
System.out.println("Object");
}
sample(String o)
{
System.out.println("String");
}


}

class constructor
{
public static void main(String arg[])
{
sample s1=new sample(null);
}
}

regards
rex
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may be because null is considered a special datatype by JAVA.
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rex tony

Originally posted by :
...I'm getting the answer is String argument.
Why the Object argument doesn't excute the first, cause Object all for the base class.
What is reason...



The reason is, for an argument type, when more than one method is found as a candidate for calling then JVM chooses the most specific one and call that method. In this example, for null value, method with String and Object argument type are both candidate for calling. Since String is more specific than Object(i.e. String extends Object) so method with String argument gets called.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moreover if you have the following code:

class sample
{
sample(Object o)
{
System.out.println("Object");
}
sample(String o)
{
System.out.println("String");
}
sample(Integer o)
{
System.out.println("Integer");
}
}

class constructor
{
public static void main(String arg[])
{
sample s1=new sample(null);
}
}


In this case you shall get the following compiler error. The reason being compiler cannot choose which constructor to call between
'sample(String o)' & 'sample(Integer o)' as because both String and Integer are child classes of class Object.

The compiler shall play safe only till it has only one specific class. As soon as it finds two classes both of which are eligible, it shall throw a compiler error.

Hope this gives you a better and clearer picture.
reply
    Bookmark Topic Watch Topic
  • New Topic