aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes super in constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "super in constructors" Watch "super in constructors" New topic
Author

super in constructors

Asha Karnataki
Greenhorn

Joined: May 13, 2000
Posts: 8
can any one explain me when exactly a call to the super class constructor is placed ?

What should be the o/p of following code
class Base1
{
void test() {
System.out.println("Base.test()");
}

}
////////////////////////////////////////////
public class Child extends Base1 {

String s = "One";
Child(int i) { test();}

Child(float f) { this ((int)f); }

void test() {
System.out.println("Child.test()");
}

static public void main(String[] a) {
new Child(10.8f).test();
}
}
Expected o/p : It should cause a compilation error as there is no default constructor in the Base class..but it does compile..why?
2. In Child(int) constructor is a call to the super class's constructor made?
3.In Child(float) constructor is a call to the super class's constructor made? if not then when exactly it is made.
I am totally confused as when exactly super() is put in the constructors. Can anyone throw light on this?
Ajay Kumar
Ranch Hand

Joined: Apr 28, 2000
Posts: 87
Hi,

Let me take UR code pieces one by one

class Base1
{
void test() {
System.out.println("Base.test()");
}
}

Since there is no constructor suppied by U with the name Base1, the compiler supplies a default constructer with no parameters like Base1(){}.


public class Child extends Base1 {
String s = "One";
Child(int i) { test();}
Child(float f) { this ((int)f); }
void test() {
System.out.println("Child.test()");
}
static public void main(String[] a) {
new Child(10.8f).test();
}
}


Here in this code U have given 2 constructors, Child(int i) and Child(float f). In this case the JVM does not supply the default constructor Child(). In the subclass the super constructor is called on the first line after the subclass constructor(Here again the compiler takes care of that unless U overtly supply a call to the super). If U do not explicitly call the super constructor the subclass constructor calls the base classes' default constructor.
So the bottom line is whenever a call to the Subclass constructor is made the base classs' constructor is automatically called.
Hope this helped.
regds
Ajay k

Regds<BR>Ajay Kumar
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello,
I've made some changes to your code by adding some prints:
class Base1{
void test() {
System.out.println("Base.test()");
}
Base1(){System.out.println("In base constructor");}
}
////////////////////////////////////////////
public class Child extends Base1 {
String s = "One";
Child(int i) {
System.out.println("In child int");
test();
}
Child(float f) {
this ((int)f);
System.out.println("In child float");
}
void test() {
System.out.println("Child.test()");
}
static public void main(String[] a) {
System.out.println("In main");
Child c = new Child(10.8f);
System.out.println("Created constructors");
c.test();
}
}
//End code
When I compile and run this, the output is :
In Main
In Base constructor
In child int
child.test()
In child float
Created constructors
child.test()

So what I could infer from this is:
First step: Main method is called (As you all know )
Second step: AS soon as you're trying to create an object of child in the main method, the base constructor is called. (Here this base constructor declaration in the Base class is not required, as its a default, but just for display purpose I've included one).
Third Step: Then the constructor (with float as arg) for child class is called. This in turn calls the constructor with int arg using this. (So the out put in this step is "in child int").

Fourth step: Since child(int) constructor calls the test() method, "Child.test()" is displayed.
Fifth Step: Control returns back to Child(float) constructor and thus prints "In child float".
Sixth Step: Created constructors displayed.
Seventh step: test() method is again called for instance c.
So answer to your question " When is super called" is that super() is called as soon as you're trying to create an instance of the child class(even before calling its own constructor).
HTH
Prabhu.

[This message has been edited by Prabhu (edited May 13, 2000).]
Raghubir Mutum
Greenhorn

Joined: May 14, 2000
Posts: 4
Hi Everyone!
Ajay and Prabhu are both right, but I want to supplement their
answers. Asha, the answer to your second question is YES, but
it is NO to your third question.
Your third question was ......
3.In Child(float) constructor is a call to the super class's constructor made? if not then when exactly it is made.
Your Child(float) constructor has a this() call, so the compiler
does not insert super() call. Remember this() and super() are
mutually exclusive as they both have to be in the first line.
The call to super() is placed only in Child(int).
Again, if you provide either this() or super() (with/without
parms) explicitly, then the compiler does not insert super().
Lets try the following variation, it'll make it a lot clearer.
Change your Child(float) to call super(float) explicitly.
Remove the this((int)f) call. This change now requires that
you explicitly define Base1(float) in the superclass. Go ahead
and make that change too.
Now, your Child class is not going to compile. The compiler
tries to insert super() in your Child(int) but does not find
Base1() in Base1 class. Base1 did not get Base1() from the
compiler as it already has an explicitly defined constructor
Base1(float). Now your Child class will compile only after
you explicitly include Base1() constructor.
Lets try this second variation too. Insert super(int) call
in your Child(int). Include the corresponding constructor
(i.e., Base1(int)) in the superclass. Now, you can do away with
Base1() in the superclass.
Thanx.
RM
Asha Karnataki
Greenhorn

Joined: May 13, 2000
Posts: 8
Thanks Raghubbir, Prabhu & Ajay.
Now I am very clear about when the call to super is made from C'tors.
Thanks a lot.
Asha
 
 
subject: super in constructors