aspose file tools*
The moose likes Beginning Java and the fly likes Calling  super()  method . Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling  super()  method ." Watch "Calling  super()  method ." New topic
Author

Calling super() method .

Praveen Kumar
Ranch Hand

Joined: Nov 06, 2006
Posts: 133
At java, We need to call super class constructor in base class constructor
at first line itself , other wise compiler will kick .

Could any one explain me why we need to put at first line itself ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
More of a beginner's question.

Look at this tutorial about the Object class, and this tutorial about super().

Imagine you have a class which is partially made up from parts unchanged from its superclass and partially from parts it has itself. To set up an object you have to set up the parts from the class and the parts from the superclass. Well, Java insists you set up the parts from the superclass first.

If you search above you find old discussion including this, this and this.
Praveen Kumar
Ranch Hand

Joined: Nov 06, 2006
Posts: 133
I have seen that constructors can not be inherited and we should not
use any instance variable and instance methods before calling super class
constructor.

Am i right ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Yes. The compiler will insist the super() call come first.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

The first line in any constructor is either a call to super(x) or to this(x). The first will call the matching constructor of the super class, with x being zero or more parameter values. The second will call the matching constructor in the same class.

If you don't put either of these in your code, the compiler will add a call to super() for you.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Praveen Kumar
Ranch Hand

Joined: Nov 06, 2006
Posts: 133
yes, can any one give stright reason why we should call them in first line ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
I presume because the designers of the language thought that creating the superclass part of the object should be done first. It also means the compiler can easily find out if you forget your "super()" call.
[ May 06, 2008: Message edited by: Campbell Ritchie ]
jaspreet atwal
Ranch Hand

Joined: Sep 05, 2007
Posts: 52
Please check you private messages.


Still Learing..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling super() method .