kar
kar

Reputation: 3651

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

Is there anything special about toString that makes it unique and more useful to Override compared to just leaving the toString method alone and create a separate method.

For example the following codes. What is the advantage/ disadvantage to Override toString method? Both the methods returns the same output.

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

public String newMethod(){
    return String.format("%s is a %s", "Apple", "fruit.");
}

Upvotes: 2

Views: 2596

Answers (13)

Jonathan Rosenne
Jonathan Rosenne

Reputation: 2217

The toString method is intended, as the Java Language Specification specifies, to provide a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person [my emphasis] to read. It is recommended that all subclasses override this method. It is useful for debugging and logging, but the result could be changed without intentionally breaking the contract. For a precise string representing the object it is best to provide a separate method, such as name in the case of enums. Such a method should not change.

The default toString is generally not so useful, therefor the recommendation to override it.

Upvotes: 0

user1073214
user1073214

Reputation: 303

One liner: System.out.println(yourclassobject) will implicitly call toString().

Upvotes: 1

elnigno
elnigno

Reputation: 1821

toString() is inherited by Object, so any object in Java can call this method.

As others said, it is used by several methods, like System.out.println(someObject).

If for some reason you really want another method doing the same that toString() does but with another name, you can do this:

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

public String newMethod(){
    return toString();
}

Upvotes: 2

Vlad Schnakovszki
Vlad Schnakovszki

Reputation: 8601

If you implement the toString() method, you can do things like:

MyType myobject = new MyType();
System.out.println(myobject);

This will automatically call the toString() method.

If you were to implement your own method, and call it newMethod(), you would have to do this:

MyType myobject = new MyType();
System.out.println(myobject.newMethod());

You can read more details about the toString() method here and here.

Upvotes: 7

BitExodus
BitExodus

Reputation: 747

The toString method is called when the instance needs to be converted to a string, for example, when you are doing something like a System.out.println(something) (in java) or Console.WriteLine(something) (in C#). It's also usefull because you can see the toString output when you are displaying variable values using debuggers.

Upvotes: 1

Alex Stangl
Alex Stangl

Reputation: 51

Within Java toString() is the standard method for converting any object to its corresponding String representation. Because this is standardized, all sorts of code can rely upon it, including debugging code that may recursively print out an object graph using Reflection.

If you want your method to be able to participate in this, then you override toString(). If, on the other hand, you are creating a method that returns some arbitrary String and you don't want it to become the object's canonical toString() representation (which presumably the existing toString() method does better), then you give it an alternative name like newMethod().

The default toString() method inherited from Object is not that useful. It just returns the object's class and hash code.

Upvotes: 2

Mario Stoilov
Mario Stoilov

Reputation: 3447

The ToString() method is a method which each and every object has. Therefore when you dont know what your object is, you can always call ToString() on it. Also, a lot of methods use the ToString() method, such as Console.WriteLine(C#) and System.out.println(Java)

Upvotes: 1

Gyan
Gyan

Reputation: 1176

Just because JVM will call an Object's toString() by default and you do not have to call it explicitly.

If you go ahead with the newMethod() implementation, you will have to print it like : sysout(objOfYourClass.newMethod());
If you override toString(), you can simply right : sysout(objOfYourClass);

Upvotes: 1

Marco Bolis
Marco Bolis

Reputation: 1220

Every object is expected to have a toString method, so you can always invoke it and expect to receive a string representation of that object.

For example System.out.println relies on it to print an object's string representation to stdout.

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

Since JVM doesn't call your custom method other than the toString() the case you call System.out.println(obj); you have to override toString()

Upvotes: 0

dcastro
dcastro

Reputation: 68670

You should do it for 2 main reasons (there may be more):

  1. It's a convention. By adhering to a convention, other users of you class will instinctively know how to use them. For example, if I wanted a string representation of a class that you built, my first thought would be to call toString on it. I would never think of calling a custom method for this.

  2. Framework classes were built with this in mind. For example, System.out.println(myObj) will call ToString on myObj.

Upvotes: 11

Martin Dinov
Martin Dinov

Reputation: 8825

Whenever the object is to be printed in it's string form, for example when you do System.out.print(object), the toString() is called. So it's a implementation convention, and you should stick to it.

Upvotes: 3

Keppil
Keppil

Reputation: 46219

The advantage to overriding is that Java automatically will use it instead of the toString() provided by Object.

Try running

System.out.println(yourObject);

with and without the

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

method and observe the difference.

Upvotes: 5

Related Questions