Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Super Keyword

  1. #1
    samuri is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default Super Keyword

    First of what are bound to be pretty stupid questions. Any help is greately appreciated.

    I'm a bit confused by the use of the super keyword in the code below

    I understand that the super keyword forces invocation of the superclass method onCreate and the reason I have to do this is because I've extended the Activity class. If I remove the super keyword, the application fails. Why?
    Do I *have* to define a subclass method if I'm going to call it? If so, why?



    public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView tv = new TextView(this);
    tv.setText("Hello, Android");
    setContentView(tv);
    }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What do you mean by fails? The exception or error message can usually do a decent job of explaining the problem. For instance, perhaps the super class needs to do something in order for the code to be executed correctly.

  3. #3
    samuri is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    I just get a 'process exited unexpectedly' in the Android Virtual machine. I'm happy to accept that the super class method needs to be called at this stage for correct execution for some reason, I'm just looking for confirmation that my assumption is correct. i.e. generally, if I make a method call when extending a class, do I *have* to define the sub class call or use the super keyword?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The best way to try it is to make an extremely simple example to test it. But if the super class method is public, you don't need to use the super keyword unless you have overridden the method in the sub class. If you have not overwritten the public super class method it will be inherited by the subclass. The super keyword is helpful when you do have an overridden version, it effectively hides the method in the super class, so the super keyword is to make the distinction to the two versions of the method.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I understand that the super keyword forces invocation of the superclass method onCreate
    Correct.

    and the reason I have to do this is because I've extended the Activity class.
    Not correct. Likewise to answer your question "if I make a method call when extending a class, do I *have* to define the sub class call or use the super keyword?": no (to either).

    Basically you have to use super.onCreate() in this particular case because the docs tell you to! Of course there is a reason for that too...

    onCreate() does all sorts of interactions with the system when it recieves the saved instance state. Some of these things the HelloWorld onCreate() can do, like setting up the UI as you do in your example. But there are basic initialisation tasks that every activity must do and these are built in to the Activity implementation of the method. The only way that these basic tasks get to be performed is if you call super.onCreate().

    You see this technique used in other places. For instance in Swing the method wherein a component paints itself might first call the super classes painting method to ensure that things like the background are done properly. But it is not a general rule: usually you do *not* have to supply an implementation and, if you do, you do *not* have to call the superclass implementation.

  6. #6
    samuri is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    Splendid, thanks. I will create some simple examples to confirm but you've answered my question there I think. Ta.

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    When you override a method, you don't need to call super(...) unless you want to do whatever the superclass method does, i.e., you're adding something to its behavior instead of replacing its behavior.

    Off the top of my head, the only time you have to call super(...) is in subclass constructors, when the superclass does not have a default (no-arg) constructor.
    Get in the habit of using standard Java naming conventions!

  8. #8
    ajayvarma is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    Hello, I am new to Java. I just wrote a simple program to check the usability of super keyword through inheritance. i get the error
    cannot find symbol.
    Symbol: variable super.
    location Class B: System.out.println("The sum of i, j, k : "+(super.i+super.j+k));
    the program I wrote is below....
    class A
    {
    int i=10,j=20;
    }

    class B extends A {
    int k=30;

    void sum()
    {
    System.out.println("The sum of i, j, k : "+(super.i+super.j+k));
    }

    }

    class Inherit1
    {
    public static void main(String[] args)
    {
    B b = new B();
    b.sum();
    }
    }

    I compiled class A.. it worked fine.
    I compiled class B... it shows the error written above.
    Please help , how could I resolve this:(

  9. #9
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    That's weird. I don't get that error. What JDK version are you using?

    Because you haven't hidden A.i or A.j by redeclaring them in B, you don't need to use super. The statement could simply be written as:

    Java Code:
    System.out.println("The sum of i, j, k : "+(i+j+k));
    If the variables were hidden, there are a couple ways you can access the values in A. Consider this:

    Java Code:
    public class B extends A {
    	int i = 0;  // hides A.i
    	int j = 0;  // hides A.j
    	int k = 30;
    
    	public static void main(String[] args) {
    		B b = new B();
    		b.sum();
    	}
    	
    	void sum() {
    		System.out.println(i+j+k); // prints 30
    		System.out.println(super.i+super.j+k); // prints 60
    		System.out.println(((A)this).i+((A)this).j + k); // prints 60
    	}
    }
    The casting technique is useful when the class that declares the variables you want to use is not the immediate superclass.
    Get in the habit of using standard Java naming conventions!

  10. #10
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Also, in the future, don't hijack threads. If you have a different question, start a new thread.
    Get in the habit of using standard Java naming conventions!

  11. #11
    ajayvarma is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    I am using jdk 6.0.. funny thing is if I write the same program in eclipse.. it runs fine without any error... but if I use a command prompt I am getting that error..... what could be going wrong ( I don't believe there could be some issue with path... as I run other programs in command prompt and they run fine... however there is one thing....
    previously I use to compile & run java programs in command prompts using the command
    "javac programname.java"
    "java programname"

    However recently I don't know what happened
    "javac programname.java" works fine...
    but when i use "java programname" command in command prompt it shows Exception in thread " main" java.lang.NoClassDefFoundError:
    but then I started using command "java -cp . programname" and the programs run fine.... could these two issues (super and unable to use the "java pgmname") be related ??? I am stumped!!

  12. #12
    ajayvarma is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    sorry about the thread.. this is the first time i have visited a forum.. I didn't know where to create so just clicked on reply message in super thread..... I found it now...

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Yep.
    I bet they are.
    Have you got a CLASSPATH declared?
    If so then I suspect that is being used when you are not supplying one directly to your javac and java calls.

  14. #14
    ajayvarma is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Yep.
    I bet they are.
    Have you got a CLASSPATH declared?
    If so then I suspect that is being used when you are not supplying one directly to your javac and java calls.
    I checked with the command echo %classpath% and it gave the reply as "C:\Program Files\Java\jre6\QTJava.zip"
    is that correct ? or is something else supposed to show up or what can i do to correct it ?

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    You don't (or shouldn't) need a CLASSPATH defined.
    It should be defined on an application basis.

    That CLASSPATH there is overriding the default (which is "current directory"), so nothing will run.
    Besides, what are you doing sticking stuff in the JRE home directory?

  16. #16
    ajayvarma is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    You don't (or shouldn't) need a CLASSPATH defined.
    It should be defined on an application basis.

    That CLASSPATH there is overriding the default (which is "current directory"), so nothing will run.
    Besides, what are you doing sticking stuff in the JRE home directory?
    Not sure what sticking stuff in JRE would mean... but I didn't set any classpath... The only thing I did to run the java on my computer is installed the Jre and Jdk 6 and appended the path of the JDK/bin directory to the path variable in the environmental variables in advanced properties of my computer.. I didn't touch any other thing... not sure how something went wrong... however any help to correct it would be great.. thank you:)

  17. #17
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    What DB said and apparently deleted -- that 'super' applies to methods, not variables -- is what I initially thought and almost answered. Maybe its use was expanded in a recent Java version. Or maybe my JDK is broken. (I'm using OpenJDK 1.6, not Sun/Oracle's.)
    Get in the habit of using standard Java naming conventions!

  18. #18
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by ajayvarma View Post
    I checked with the command echo %classpath% and it gave the reply as "C:\Program Files\Java\jre6\QTJava.zip"
    is that correct ? or is something else supposed to show up or what can i do to correct it ?
    QTJava.zip is the QuickTime library. I think the QuickTime installer adds it to your system classpath (not sure whether it asks permission). This causes trouble for all kinds of Java installations. You can either delete it, or, if you think you need it, just prepend the current directory (.) to it:

    ".;C:\Program Files\Java\jre6\QTJava.zip"

  19. #19
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by kjkrum View Post
    What DB said and apparently deleted -- that 'super' applies to methods, not variables -- is what I initially thought and almost answered. Maybe its use was expanded in a recent Java version. Or maybe my JDK is broken. (I'm using OpenJDK 1.6, not Sun/Oracle's.)
    'super' is the local reference to the superclass, the superclass equivalent of 'this'. Access to superclass variables via 'super' will be determined by their access specifiers, but it's use is redundant unless they're hidden by class or local variables.

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Quote Originally Posted by dlorde View Post
    QTJava.zip is the QuickTime library. I think the QuickTime installer adds it to your system classpath (not sure whether it asks permission). This causes trouble for all kinds of Java installations. You can either delete it, or, if you think you need it, just prepend the current directory (.) to it:

    ".;C:\Program Files\Java\jre6\QTJava.zip"
    Ah!
    I had heard that one...pain in the arse.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 06-03-2011, 09:15 PM
  2. rule for using the super keyword
    By javastuden in forum New To Java
    Replies: 2
    Last Post: 10-01-2010, 09:02 AM
  3. calling variable using super super..
    By Stephen Douglas in forum New To Java
    Replies: 7
    Last Post: 08-16-2010, 06:12 AM
  4. this keyword
    By coltragon in forum New To Java
    Replies: 10
    Last Post: 03-01-2010, 09:20 AM
  5. Use of this keyword
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-18-2007, 07:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •