I am using Eclipse Build id: 20120614-1722.
I have an object class called “TOY1” and a function toString() within it. I previously know that when I call the
System.out.println(TOY1);
It should return the address. However, and for some reason it is returning the toString() declared for my object without me specifying the @Override notation.
Is it safe to keep it that way? Or is this a new feature implemented in the specific build I have.
Thanks
EDIT
As asked this is part of my code:
public class TOY1 {
//irrelevant declarations
public String toString() {
String data;
data="Manufacturer=";
data+=manufact;
data+="DOP:";
data+=date_of_production;
return data;
}
}
When declaring TOY1_instance of TOY1 and then printing out using the System.out.printIn(TOY1_instance)
I am getting the actual data as opposed to some junk address.
My question is where did I override it no warning was shown and no extension overrides this class.
System.out.println(obj);is callingobj.toString()internally. It happens that the default implementation oftoString(), if you don’t override it, returns some address-like value.You can omit
@Overrideannotation, but it’s safer to use it. It becomes especially useful when you think you are overriding while you aren’t because of tiny difference in signature. E.g.:won’t compile. Even more common mistake is wrong
equals():Do you see why? Without
@Overrideit’s very easy to miss such a tremendous bug.