Simple Java Question

Status
You're currently viewing only wb's posts. Click here to go back to viewing the entire thread.
Not open for further replies.

wb

Ars Legatus Legionis
25,208
Subscriptor
You'll see super() used occasionally outside constructors. Usually happens when you override a method in a class you're extending but still want the original parent method to be called.


Show me working source code that actually does that. So far as I know super can only be used as the first call in a constructor. Your example doesn't compile; I made a test case just to be sure.
 

wb

Ars Legatus Legionis
25,208
Subscriptor
I just want to make it clear to everyone that there are two completely different cases where the super keyword gets used.

The super() method is used to explicitly invoke a base class constructor. The only time this is useful is when you explicitly want to call a base class constructor other than the default constructor (i.e. one that takes arguments). Otherwise, the default constructor super() is implicitly invoked anyway. This method cannot be called except for as the first call in a constructor.

The other usage of keyword is as a "shorthand" for ((BaseClassName) this) that you use to access base class methods or variables. I think the proper term is "shadowed" variables and methods. One trivial use for this is to differentiate between two variables or methods with the same name in both the base and derived classes.
 
Status
You're currently viewing only wb's posts. Click here to go back to viewing the entire thread.
Not open for further replies.