java - Why must I override toString method instead of just creating another method? -


Is there a special thing about the string that is unique and more for override than leaving the toString method alone? Useful method differently

For example, what is the benefit / non-profit for the following code overstream tooling method? Both methods give the same output.

  @Override public string toString () {String.format ("% s is a% s", "Apple", "fruit."); } Public string newMethod () {return string. Format ("% s is a% s", "Apple", "fruit."); }  

If you apply the toString () method You can work like this:

  MyType myobject = new MyType (); PrintLn (MyObject);  

This will automatically call the toString () method.

If you implement your own method, and call it NewMethod () , you have to:

  myType myobject = New MyType (); Println (myobject.newMethod ());  

You can read more information about the toString () method and


Comments