public class Curiosity {
public void toString()//error because of this specific method name
{
System.out.println("method is successfully implemented");
}
}
How can i use a method of the same name "toString()" if i want to ?
Do I have to give its return type as String if not what should i do to change its return type like suppose if i want to use a void return type for toString does java allow that ?
toString()
method must return aString
. That's the only way to overrideObject
'stoString()
.If you wish to use the same name but not override
Object
'stoString
, you can overload the nametoString
by adding arguments, thus changing the signature of your method.Example :