Regarding question

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
kk2457
Posts: 1
Joined: Tue Oct 01, 2013 10:27 am
Contact:

Regarding question

Post 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?
Last edited by admin on Tue Oct 01, 2013 11:12 am, edited 1 time in total.
Reason: Added [code] tags. Please specify Question ID as well.

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Regarding question

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests