If I have a method in my subclass returning a String and I
make>>>>> return super
why does not the compiler call toString() method ?
is it not equal to keyword "this"?
Tymoteusz Gedliczka - 22 Mar 2007 13:34 GMT
> If I have a method in my subclass returning a String and I
> make>>>>> return super
> why does not the compiler call toString() method ?
> is it not equal to keyword "this"?
Keyword super by itself doesn't reference to any object, nor call any
method, so you can't just "return super;"
You can use super in two ways:
- to call constructor of the superclass, call super(). There are,
however, limitations: you can use it only in first line of any
constructor. Definitely, it is useless for your purpose.
- to reference some method from superclass you use syntax:
super.methodFromSuperClass()
What you probably want to do is to call:
return super.toString();
Oliver Wong - 22 Mar 2007 16:04 GMT
> If I have a method in my subclass returning a String and I
> make>>>>> return super
> why does not the compiler call toString() method ?
> is it not equal to keyword "this"?
Notice that the "this" keyword does not auto-invoke toString() or anything
like that. The following code will NOT compile:
public class Foo {
public String methodReturningString() {
return this;
}
}
- Oliver
Mark Rafn - 22 Mar 2007 19:05 GMT
>If I have a method in my subclass returning a String and I
>make>>>>> return super
>why does not the compiler call toString() method ?
>is it not equal to keyword "this"?
This would be clearer if you included a tiny example that showed what you want
to do. But the answer is twofold:
1) super is not a reference to a type, it's a way to un-override uses of
methods and members. this.foo() and super.foo() both call foo() on the
"this" object, but super.foo() runs the code in the superclass rather than
in this class. You can't return super in the same way you can return this,
because there is no distinct super object, it's effectively just part of this.
2) You can't return an object (not even "this") from a method that returns
String anyway. You can only return a String. You can return this.toString(),
or you can return super.toString().
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>