• Post Reply Bookmark Topic Watch Topic
  • New Topic

about Constructor overloading  RSS feed

 
Prasath Thirumoorthy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
consider the following code
*******************************************************
public class SpecificMethodExample {

public SpecificMethodExample() {
System.out.println("Constructor with no Argument is called...");
}
public SpecificMethodExample(String str){
System.out.println("Constructor with String Argument is called..."+str);
}
public SpecificMethodExample(Object obj){
System.out.println("Constructor with Object Argument is called..."+obj);
}
public static void main(String[] args) {
SpecificMethodExample sme = new SpecificMethodExample(null);
}

}
***********************************************************
i dont know y the constructor which has string as argument is invoked when the code is executed.y not the constructor which has object as argument is called since its it is super most class i want to know the reason.plz any help me
with regards
prasath
 
Jack Kay
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey prasath! How are you?

The answer to your question is that it's because the String is at the top of the inheritance tree. It's like this:

-Object
--String(implements Object)

So the method that takes the String gets called.

If you want to call the public SpecificMethodExample(Object obj) method, try going like this:


If for some reason you're doing this:


Then if you need to pass null to either one, ESPECIALLY since there is no inheritance between either one, you'd go like this:


So it all comes down to casting the null to whatever constructor you want Have a nice day
[ August 11, 2004: Message edited by: Jack Kay ]
 
jim gemlo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String extends Object not implements object.
 
Prasath Thirumoorthy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Jack im fine how abt u.
Thank u very much for ur response
still im not clear.Actually u told String is in the top of Inheritance but Object is the supermost class.y we need xplicit type conversion to call the object constructor.Actually in java for primitive data type lower data type if want to convert int to short we need to type cast explicitly but in this case it gets reversed thats y i got confused can u provide some more explanation plz help me friend for better understanding
with regards
prasath
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Actually in java for primitive data type lower data type if want to convert int to short we need to type cast explicitly but in this case it gets reversed thats y i got confused can u provide some more explanation plz help me friend for better understanding"

I've studied Latin and Englis but that didn't help me here.
Please use proper punctuation, grammar and spelling so people can understand your questions if you want meaningful answers.

When overloaded functions are called (and constructors too) the one with the arguments that best match the ones supplied is picked.
So if you have a class X deriving from Y which derives from Z and you have 3 constructors in class A:

and you then call A a = new A(new X()); it calls A(X).
If you had used A a = new A(new Y()); A(Y) would have been called.
Now if you have Z x = new X(); A a = new A(x); it calls A(Z) because you call it with a Z not an X. Had you used A a = new A((X)x); it would have used A(X) instead as expected.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!