+ 2

Why is this code outputting x?

I really don't get it. https://code.sololearn.com/caBj7McKlMbt/?ref=app

29th Jul 2017, 9:22 AM
Jonas Schröter
Jonas Schröter - avatar
5 odpowiedzi
+ 9
Do not override toString() method...
29th Jul 2017, 9:32 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 3
Yes, I realised it after @ValentinHacker posted.
29th Jul 2017, 9:38 AM
Jonas Schröter
Jonas Schröter - avatar
+ 2
This is was challenge question, but now I understand. Thank you.😊
29th Jul 2017, 9:34 AM
Jonas Schröter
Jonas Schröter - avatar
+ 2
in System.out.print(new A(10)); toString method is implicitly called. toString method is part of class A. so you get the output "x"
29th Jul 2017, 9:36 AM
Devbrath
Devbrath - avatar
0
toString method is part of the object class. it is always implicitly called in System.out.println(). Object class is like the father of all class. this class is inherited by all class
29th Jul 2017, 9:37 AM
Devbrath
Devbrath - avatar