Home arrow static arrow Java Programming [Archive] - Question regarding Constuctors!!!
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Question regarding Constuctors!!!
This topic has 7 replies on 1 page.

Posts:754
Registered: 6/13/03
Question regarding Constuctors!!!  
Jul 16, 2004 2:53 AM



 
Hi,

I have a question regarding Constuctors.
Look at the following code:

class Base{        Base(){	System.out.println("Base");    }}public class In extends Base{    public static void main(String argv[]){	In i=new In();    }}  


The output of this is : Base

The class In extends Base.right?
Now,we create a new instance of In.

My question is:
We have not defined a constructor for class 'In'.
so it has a default constuctor.

So how does the the child class create a constructor
which calls super()?
We have not specified super anywhere?

Can some one explain or am I overlooking some point?
 

Posts:1,995
Registered: 12/10/00
Re: Question regarding Constuctors!!!  
Jul 16, 2004 3:00 AM (reply 1 of 7)



 
The [url http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#16823]JLS[/url] can. A superclass constructor or another constructor of this class must be invoked as a first statement of the constructor body. There are some exceptions with initializing private fields of inner classes, but that is not the case.

Mike
 

Posts:342
Registered: 5/22/98
Re: Question regarding Constuctors!!!  
Jul 16, 2004 3:00 AM (reply 2 of 7)



 
Not only is a default constructor created for you, but any constructor (including the default one) without a super call will automatically call super()

Graeme
 

Posts:50
Registered: 7/16/01
Re: Question regarding Constuctors!!!  
Jul 16, 2004 3:02 AM (reply 3 of 7)



 
Hi,

when you define a class like In without explicitly definining a constructor, a default constructor. The default constructor takes no parameters and invokes the superclass constructor with no arguments.

Stefan
 

Posts:24
Registered: 6/24/03
Re: Question regarding Constuctors!!!  
Jul 16, 2004 3:02 AM (reply 4 of 7)



 
The default constructor of the derived class irrespective of whether it has been explicitly defined or not, calls the base class constructor before getting called.

This is by OO concepts that a derived object cannot be created without creating its parent! And, when the parent gets created, its default constructor gets called automatically.

Hope this helps!
 

Posts:6,487
Registered: 5/5/04
Re: Question regarding Constuctors!!!  
Jul 16, 2004 3:04 AM (reply 5 of 7)



 
The class In extends Base.right?
Yes.

So how does the the child class create a constructor which calls super()?
The compiler does this when it compiles the java.

We have not specified super anywhere?
Correct. This is unavoidable. super( /* something */ ) is always called.
Only java.lang.Object does not call its super class.

Can some one explain or am I overlooking some point?
I don't understand the confusion.
Are you expecting it to be more complicated?
 

Posts:98
Registered: 6/8/04
Re: Question regarding Constuctors!!!  
Jul 16, 2004 4:19 AM (reply 6 of 7)



 
When have inheritence as in this case are two instances created then i.e. Base and In or is only the class of Base created and the class and instance of In created? If that makes any sense :-/
 

Posts:6,487
Registered: 5/5/04
Re: Question regarding Constuctors!!!  
Jul 16, 2004 4:25 AM (reply 7 of 7)



 
If you create a new In() you will create one object.
In newObject = new In();Base baseObject = (Base) newObject;System.out.println("In = "+ newObject);System.out.println("Base = "+ baseObject);

This will printout a reference to the same object.
 
This topic has 7 replies on 1 page.