This discussion is archived
5 Replies Latest reply: Apr 21, 2013 8:42 AM by baftos RSS

toString method why its not called implicitly for objects

997192 Newbie
Currently Being Moderated
class Building { }
public class Example
{
public static void main()
{
Building b = new Building();
String s = (String)b;
}
}

in the 5th Line it shows error that cannot cast building type to string..
but all classes have the toString method which is called whenever we give the object arguement in System.out.println();

the following code compiles

class Building { }
class Example
{
public static void main()
{
Building b = new Building();
System.out.println(b);
}
}
can anyone explain this?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points