Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question required

 
Inuka Vincit
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your wondering why toString is called. When you called print or prinln on an object the toString method of that object is called.
Here is the doc


print

public void print(Object obj)

Print an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Parameters:
obj - The Object to be printed
See Also:
Object.toString()


here is the link to the doc
http://java.sun.com/j2se/1.3/docs/api/java/io/PrintStream.html
 
PETER CARTER
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A {
public String toString () {
return "4";
}
}
class B extends A {
public String toString () {
return super.toString() + "3";
}
}
public class test {
public static void main(String[] args) {
System.out.println(new B());
}
}
Compilation succeeds and 43 is printed.
But why ?
Thanks !!
 
Colin Fletcher
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return super.toString() + "3"

Tells the class to call the super (A) toString method, which returns "4". "4" + "3" = "43"
 
natarajan raman
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!!!That's really helpful Inuka.....
 
VINCE CARTER
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic