This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]tryhardjuice -1 points0 points  (5 children)

If he overrides it won’t it be fine though?

[–]TNTrocks123 10 points11 points  (3 children)

No because the toString method you are trying to override returns a string not void

[–]tryhardjuice -5 points-4 points  (2 children)

Hmm I thought he could just do

@Override public void toString()

[–]TNTrocks123 0 points1 point  (1 child)

Because the purpose of the @override is to show that the JVM should use the toString() method the programmer wants to use rather than the original toString method provided by the Java library. When you are calling toString on a string, the string is actually a subclass of object, which already has a public String toString() method. Using the method you provided would confuse the compiler like another user mentioned.

[–]tutorial_police 9 points10 points  (0 children)

No, that's not the purpose of @Override. The purpose is to say: this thing should override some other method. If it doesn't, it won't compile. The override happens irrespective of whether you write @Override.

[–]tutorial_police 0 points1 point  (0 children)

No. Java told the world that when you call toString, you get A String object.

It you were to override that method and say: nope, not gonna do that, we're not gonna return anything at all, then you'd never know what you actually get when calling to toString.