This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hey friends !
I want know Can we make a call to super class method using keyword "super . METHOD_NAME" from outside the constructor ?
I read it somewhere we can't call super(); outside the constructor .
Kindly clarify my doubt .
You need this kind of call when you override a method and you want the method to do something in addition to the superclass method. So you say: "Do the stuff defined in the superclass and then something in addition.
If you just call super(); then this is a call of the superclass constructor and that can be only done from within a subclass constructor. Even more specific it has to be the first statement in the subclass constructor.
super.<method-name> (or) super.<field-name> from the instance methods and constructor... but not in static methods...
And super() can be called only in the constructor that too as the first statement...
The fact is super() is to indicate that the initialization of inherited super class properties should be initialized before the sub-class properties are initialized so it can be done only in the constructor...
super() (or) super(args) to invoke the super class constructor in first statement of sub-class constructor...
"super." keyword used to invoke super class methods (or) access super class variables that too in instance methods only
The best thing you can do, whenever you have a question "can I do ...", is to try it out. You'll remember it better that way, and it's both faster and more reliable than asking in a forum. What forums are good for is helping you understand why it works like that.