• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

super or super( )

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic