Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override of toString() that makes use of the overridden toString()

Basically this is what I am trying to achieve.

classname@address(?)[original toString()], object's name, object's age

@Override public String toString()
{
return String.format("%s , %s , %d", this.toString(), this.getName(),this.getAge());
}

Problem, toString() gets called recursively.

I can't call super.toString() because that's not what I want. I want 'this' to call the original toString().

This

this.super.toString() 

doesn't work for obvious reasons.

I'm running out of steam, can this be done once a method gets overridden? I.e. call the original implementation ?

(my edit) Basically what I need is: override toString to display two properties of 'this' object, and I also want to have 'this' call the original toString.

This is not a project , or work. Just playing(learning) with Java language. Thanks

like image 373
Marin Avatar asked Dec 12 '11 22:12

Marin


People also ask

What happen if we override toString () method?

By overriding the toString( ) method, we are customizing the string representation of the object rather than just printing the default implementation. We can get our desired output depending on the implementation, and the object values can be returned.

Why do we override toString ()?

Override the toString() method in a Java Class A string representation of an object can be obtained using the toString() method in Java. This method is overridden so that the object values can be returned.

Can you override toString?

We can override the toString() method in our class to print proper output. For example, in the following code toString() is overridden to print the “Real + i Imag” form.


1 Answers

You're looking for super.toString().

like image 53
SLaks Avatar answered Nov 14 '22 23:11

SLaks