Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Annotations Using Java's Built-In Annotations The @Override Annotation

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

@Override question ?

Hey folks,

do we need to @override methods like toString() to avoid runtime errors?

In this video we put a String argument into toString method, without making a constructor that takes a parameter String.

Then we compile the java file where a new Cheese object is created and the compiler gives us a hashcode of the Cheese object ...

And why do we need to override the toString() method ????

Thanks

Grigorij

3 Answers

deckey
deckey
14,630 Points

Hi Grigorij, as Jeremiah explained, toString() method exists on all objects, since it exists on a superclass Object that all inherit from. But, default toString() method is not very descriptive to the user, giving 'name of the class @ memory location' when printed, like:

cheese.Cheese@6276e1db

To be exact, it is:

return getClass().getName() + "@" + Integer.toHexString(hashCode());

Now, if presenting our instances is important, then we can @Override the method to something like:

@Override
public String toString(){
   return "This is an instance of "+this.getClass()"
}

hope it makes sense, good luck!

Hi Grigorij,

Short:
If we'd be coding the toString() method without an @Override annotation, it would work anyway as it just has to return a String. The @Override is just used to give the compiler the chance to notify you of possible errors.

Long:
The toString() method was just a handy example on what we could perform the @Override annotation to see how it works. When compiling toString() with a String parameter (and without a Override annotation) the compiler assumes it to be a new method (or an overload of the toString-method, which just isn't used anywhere). For converting objects as Strings the normal toString() method is still used at this point.

If we are now telling the compiler to check if our toString(String something) correctly overrides an inherited method (that could even be a custom toString() method of the superclass), it'll notify us if there has no method been inherited from with this signature.

Hope this helps & best regards,
Michael

Hi Grigorij,

Every class has the methods that are arrived parent class, which would be Object. An Object class has, by default, methods pre built such as toString(). The @override line tells the compiler to use the toString() method we created rather than the default toString() method.

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Jeremiah,

but what does the default toString method that makes it to bee overridden???