when you say print(t) it is same as print(t.toString()). toString() is a method from Object class.If you do not add this toString() compiler will do that for you. Implementation given in the Object class toString method is given below
that's why it is printing like that.
for String object it print the value because in String class they have over ridden the Object class toString() method and they have given their own implementation.
thanks for the reply friends, but what i am not getting is that in main when i am creating
i am passing t as an argument.Now this t is of derived type so it should have its default value as null, and when i m passing this null value to constructor
it will assign this null value to this.t and then in both the print statements it should have printed null. but it is printing different thing so i want to ask that why is the default value of t null is not getting passed to B(Test t).
thanks friend. so you mean to say that when we create a new object with some reference than that reference doesnt have any value. but then why is it printing null in first print statement. . . . . . . . i am totally confused in this question.please explain it clearly....
The Test reference that is declared as a instance variable in A has nothing to do with the Test reference that is instantiated in your main method. They just happen to have the same name. So when the constructor for A runs, the Test reference that is an instance variable in a is null, but then you pas in a valid, initialized test reference, and you assign the instance Test reference to it. So now both Test references happen to have the same name 't' and also point to the same object.