I'm studying java and have a question about toString method. In both case:
//**Case1: **
@Override
public String toString(){
return "Hello world";
}
//**Case2:**
public String toString(){
return "Hello world";
}
So my question is when should we need to include the @Override. Because basically the case2 is Override it already. Why we need to @Override it again.
Understanding how toString work
@Overrideneed to be included always for thepublic String toString()method, sincejava.lang.Objectcontains this method and all classes inherit fromjava.lang.Object.@Override, this provides a compile time check to ensure that you're actually doing what you think you're doing (and don't end up wondering why your code doesn't work as expected)