6

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

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Marin
  • 12,531
  • 17
  • 56
  • 80
  • 4
    If you override the `toString()` method then there is no other implementation than the one you create. What exactly is wrong with calling `super.toString()`? – Hunter McMillen Dec 12 '11 at 22:32
  • 1
    Have you tried `super.toString()` (without the `this`)? – NPE Dec 12 '11 at 22:33
  • A possible duplicate http://stackoverflow.com/questions/586363/why-is-super-super-method-not-allowed-in-java – GETah Dec 12 '11 at 22:34
  • By the original `toString()`, do you mean a previous version of this exact method in this exact class, or the `toString()` from the superclass? – Allen Z. Dec 12 '11 at 22:35
  • @Marin You say you don't want to call super.toString(), you want the instance to call super.toString() ... if you have the toString() overriden method call super.toString() then you ARE having the instance call the base implementation. Either you are trying to describe some other desired effect or just beating your head over nothing. – Matthew Cox Dec 12 '11 at 22:35
  • @Allen Z ,@ Matthew Cox Yes I want the toString from the superclass. I think I just need super.toString() but I tried it and I don't think it worked. I will test again. – Marin Dec 12 '11 at 22:36

2 Answers2

10

You're looking for super.toString().

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Hello SLaks, I want 'this' object to call super.toString(). Is that the same thing? If I call super.toString() does it know that it's 'this' object calling super.toString()? thanks – Marin Dec 12 '11 at 22:34
  • 1
    @Marin: `super` is `this`. It is impossible to call the super's `toString` on any other instance. – SLaks Dec 12 '11 at 22:35
  • 5
    @Marin `this` refers to the current object, `super` refers to the current object's parent class. So "this" classes parent is `super` – Hunter McMillen Dec 12 '11 at 22:35
  • The caller of a method does not matter. The method will always do the same thing, whatever the caller of the method is. toString will be invoked on this, even if you call super.toString(). It's just the code of the overridden toString that will be invoked, on 'this'. Just because you call a superclass method doesn't change the object on which it is called. The same object is both of type SubClass and SuperClass. – JB Nizet Dec 12 '11 at 22:37
  • @JBNizet thanks JB. So if I'm getting things straight, super is 'this' object that calls the superclasses method? – Marin Dec 12 '11 at 22:45
  • super is 'this' object **on which** the superclass method is called. The caller of the method could be any other object or static method. You're confusing the caller of a method and the object on which a method is called. – JB Nizet Dec 12 '11 at 22:51
0

Actually, I was trying to achieve the same thing, where super.toString() wasn't exactly what I wanted. I believe that this is not possible.

Specifically, I have a domain class in Groovy/Grails, and I wanted to override its toString() method to add some extra information:

class Child extends Parent {

    public String toString() {
        return super.toString(this) + extra_info // not possible!!!
    }
}

This is not identical to return super.toString() + extra_info. In one case I get e.g.

com.example.domain.Parent : 15, extra_info. (Wrong. The instance is the same, but toString includes type information.)

In the other:

com.example.domain.Child : 15, extra_info. (Correct, but not possible.)

Sergey Orshanskiy
  • 6,794
  • 1
  • 46
  • 50