Results 1 to 20 of 24
Thread: Super Keyword
- 07-27-2011, 11:14 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
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);
}
}
- 07-27-2011, 11:33 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 07-27-2011, 11:57 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
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?
- 07-28-2011, 12:08 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 07-28-2011, 12:13 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Correct.I understand that the super keyword forces invocation of the superclass method onCreate
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).and the reason I have to do this is because I've extended the Activity class.
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.
- 07-28-2011, 12:14 AM #6
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
Splendid, thanks. I will create some simple examples to confirm but you've answered my question there I think. Ta.
- 07-28-2011, 01:04 AM #7
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!
- 07-28-2011, 05:56 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 10
- Rep Power
- 0
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:(
- 07-28-2011, 06:15 PM #9
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:
If the variables were hidden, there are a couple ways you can access the values in A. Consider this:Java Code:System.out.println("The sum of i, j, k : "+(i+j+k));
The casting technique is useful when the class that declares the variables you want to use is not the immediate superclass.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 } }Get in the habit of using standard Java naming conventions!
- 07-28-2011, 06:16 PM #10
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!
- 07-28-2011, 06:23 PM #11
Member
- Join Date
- Jul 2011
- Posts
- 10
- Rep Power
- 0
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!!
- 07-28-2011, 06:27 PM #12
Member
- Join Date
- Jul 2011
- Posts
- 10
- Rep Power
- 0
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...
- 07-28-2011, 06:29 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
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.
- 07-28-2011, 06:34 PM #14
Member
- Join Date
- Jul 2011
- Posts
- 10
- Rep Power
- 0
- 07-28-2011, 06:36 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
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?
- 07-28-2011, 06:43 PM #16
Member
- Join Date
- Jul 2011
- Posts
- 10
- Rep Power
- 0
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:)
- 07-28-2011, 07:06 PM #17
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!
- 07-28-2011, 07:32 PM #18
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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"
- 07-28-2011, 08:18 PM #19
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
- 07-29-2011, 09:21 AM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
Similar Threads
-
Can you use the super keyword when variables in parent class are private?
By lam5442 in forum New To JavaReplies: 2Last Post: 06-03-2011, 09:15 PM -
rule for using the super keyword
By javastuden in forum New To JavaReplies: 2Last Post: 10-01-2010, 09:02 AM -
calling variable using super super..
By Stephen Douglas in forum New To JavaReplies: 7Last Post: 08-16-2010, 06:12 AM -
this keyword
By coltragon in forum New To JavaReplies: 10Last Post: 03-01-2010, 09:20 AM -
Use of this keyword
By Java Tip in forum Java TipReplies: 0Last Post: 11-18-2007, 07:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks