• Post Reply Bookmark Topic Watch Topic
  • New Topic

super or super( )  RSS feed

 
O Joseph
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
please save me b4 i get totally confused: i have seen questions in
different mock exams using super( ) to invoke a method or even a variable
from a superclass to a subclass but at the same time some say this use is
wrong and should only be used in constructors.plz react.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok -- so, lets see what I can remember about all this super stuff...
super() -- is only used in a constructor.
super.something -- can be used anywhere in a class (but its usually used when you override a method from the superclass)
When you create a subclass, by default, the compiler will insert "super();" as the first line of the constructor -- if its not there already. The only time it won't insert "super()" is if the first line of the constructor is "this()" or some other form of "super(someVariable)"
Lets try an example, that usually helps (read through the comments and then run the code):

So... does that help? or just confuse the situation even more?
[ October 04, 2003: Message edited by: Jessica Sant ]
 
O Joseph
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jessica Sant:

super() -- is only used in a constructor.
super.something -- can be used anywhere in a class (but its usually used when you override a method from the superclass)
...So... does that help? or just confuse the situation even more?
[ October 04, 2003: Message edited by: Jessica Sant ]

thanks j, i think u've armed moi with what i supposed was true before coming across some mock questions whose answer suggested that super() could also be used to override a method from a superclass as long as it wasn't used in a constructor. ( i thunk i saw something similar to dat also on one of marcus's question that an old friend gave moi - i'll try and check moi stuff if i still lay moi hands on it; just to serve as a confirmation and reference).
from what u've said now, the bottomline is that super() is only used in constructors.
thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!