Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

keyword super

Thread view: 
josh - 22 Mar 2007 12:59 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"?
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/>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.