Page 1 of 1
Regarding question
Posted: Tue Oct 01, 2013 10:30 am
by kk2457
Code: Select all
class Super
{
public String toString()
{
return "4";
}
}
public class SubClass extends Super
{
public String toString()
{
return super.toString()+"3";
}
public static void main(String[] args)
{
System.out.println( new SubClass() );
}
}
For the above, ethuware states the answer is 43, but why is it 43 when the new SubClass did not even call the toString method nor is in the constructor, is it an error?
Re: Regarding question
Posted: Tue Oct 01, 2013 11:11 am
by admin
The println method calls toString on the object that you passed in as an argument. So toString() will be called on new SubClass() object and that value will then be printed.
HTH,
Paul.