Question regarding how Object internally behave in java

Hi ,

I am trying to run this sample code and i also got the result of this and the result is String--> null , but question is that why it call the parameterized constructor of the String type not Object type ?I know that Object is the super class of all the class(that means it is the superclass of String class also) and that's y here control is first goes to subclass type then superclass type.

Can some body pls give me the suggestion , how it internally work?

public class string_test {

string_test(Object obj)

{

System.out.println("Object--> "+obj);

}

string_test(String str)

{

System.out.println("String--> "+str);

}

public static void main(String args[])

{

string_test ob=new string_test(null);

}

}

[819 byte] By [Dilip_biswala] at [2007-11-27 2:28:11]
# 1

Partially right. Remember that overloading (which you are doing) is analyzed at *compile* time, whereas overriding is determined at run-time. If you look at the program below and its output, you should get a better understanding.

Java will attempt to overload to the most specific sub-class possible.

final class Foo extends Object {

private Foo() {

super();

}

public static final void main(final String[] args) {

// Here is your original string test. At compile-time, it is declared a string.

String testString = null;

foo(testString);

// Prints as expected

java.util.Date testDate = null;

foo(testDate);

// Prints as expected

java.sql.Date testSqlDate = null;

foo(testSqlDate);

// Shows how overloading is compile time

java.util.Date testDateInheritance = new java.sql.Date(new java.util.Date().getTime());

foo(testDateInheritance);

// Java matches the best possible overloading, so only Object applies here

Integer testOther = null;

foo(testOther);

}

private static void foo(final String target) {

System.out.println("Foo as String");

}

private static void foo(final java.util.Date target) {

System.out.println("Foo as vanilla date");

}

private static void foo(final java.sql.Date target) {

System.out.println("Foo as SQL date");

}

private static void foo(final Object target) {

System.out.println("Foo as Object");

}

}

OUTPUT:

Foo as String

Foo as vanilla date

Foo as SQL date

Foo as vanilla date

Foo as Object

- Saish

Saisha at 2007-7-12 2:39:53 > top of Java-index,Java Essentials,Java Programming...
# 2

And the reason that it directs to the method with the most specific argument when you give it a null is because to the compiler null acts as the "subclass of everything". Kinda like a reverse object.

If you have add a method there that takes an Integer argument and try to pass null, the compiler complains about ambiguity since it can't decide whether null should be Integer or String.

-Kayaman-a at 2007-7-12 2:39:53 > top of Java-index,Java Essentials,Java Programming...
# 3
Thank you very much.I got it
Dilip_biswala at 2007-7-12 2:39:53 > top of Java-index,Java Essentials,Java Programming...
# 4
Glad we could help. Best of luck.- Saish
Saisha at 2007-7-12 2:39:53 > top of Java-index,Java Essentials,Java Programming...