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…
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)- A11% (7)
- B6% (4)
- C3% (2)
- D77% (49)
- E2% (1)
- F2% (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
Community Discussion
No community discussion yet for this question.