nerdexam
Oracle

1Z0-804 · Question #106

To provide meaningful output for: System.out.print( new Item ()): A method with which signature should be added to the Item class?

The correct answer is D. public String toString(). Implementing toString method in java is done by overriding the Object's toString method. The javatoString() method is used when we need a string representation of an object. It is defined in Object class. Thismethod can be overridden to customize the String representation Below…

Java Class Design

Question

To provide meaningful output for:

System.out.print( new Item ()):

A method with which signature should be added to the Item class?

Options

  • Apublic String asString()
  • Bpublic Object asString()
  • Cpublic Item asString()
  • Dpublic String toString()
  • Epublic object toString()
  • Fpublic Item toString()

How the community answered

(64 responses)
  • A
    11% (7)
  • B
    6% (4)
  • C
    3% (2)
  • D
    77% (49)
  • E
    2% (1)
  • F
    2% (1)

Explanation

Implementing toString method in java is done by overriding the Object's toString method. The javatoString() method is used when we need a string representation of an object. It is defined in Object class. Thismethod can be overridden to customize the String representation Below is an example shown of Overriding the default Object toString() method. The toString() method must bedescriptive and should generally cover all the contents of the class PointCoordinates { private int x, y; public PointCoordinates(int x, int y) { public int getX() { public int getY() { // Custom toString() Method. public String toString() { return "X=" + x + " " + "Y=" + y;

Topics

#toString#Object methods#print output#method override

Community Discussion

No community discussion yet for this question.

Full 1Z0-804 Practice