Author Topic:   Confused help me!!
sree
ranch hand
posted March 29, 2000 04:14 PM             
Hi Guys,

I am confused with the follwing code.

code:

class Base{
Base(int i){
test();
}
void test() {
System.out.println("Base.test()");
}
Base(){
this(5);
System.out.println("base constructor called");
}
}

public class Child extends Base {
Child(int i){
test ();
}

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

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

static public void main(String[] a) {
Child y =new Child(10.8f);
}
}



The output is
Child.test()
Base construtor called
Child.test().

Why not?
Base.test()
Base construtor called
Child.test().

Thanks.

[This message has been edited by maha anna (edited March 29, 2000).]

maha anna
bartender
posted March 29, 2000 09:54 PM             
sree,
I purposly waited for someone to initiate. So here is some help.
I am using a general format for explaining this.

Step1 :
You make new Child(float f) . This form of Child class's constructor diverts to Child(int i) form of constructor by calling this((int)f);

Step2 So the foll constructor is called.


Child(int i){
test ();
}

which is same as


Child(int i){
super();
test ();
}


Step 3
Since super() is implicitly provided by the compiler as the FIRST statement in Child(int i){.. } constructor,the super class Base() { } constructor called. This diverts to Base(int i) {..} constructor.

Step 4
In Base(int i) { } version calls test() method, since we are started creating new Child(..) from the main method, the overridden version of the test() method in Child class is executed which prints the first line of output Child.test()

Step 5
Then after finishing the Base(int i) { } version constructor, control comes back to Base() {..} which prints the 2nd line of output base constructor called

step 6
Now control comes back to Child(int i) { } version, and executes the next statement test() (same execution flow as was in Base() constructor) is executed , which calls the overridden version of the test() in Child class and prints the 3rd line of output Child.test()

I hope this helps. For simplicity I created more steps. But when you understand the concept, just looking itself you can follow the execution flow.
regds
maha anna

Umesh
ranch hand
posted March 29, 2000 10:46 PM             
Anna,

What is the compiler rule to provide super() in the subclass constructor ?
Is that the following:
Compiler provides super() in the first line for all the contructor except for those don't implicit call for its own constructor like this().
Does compiler provide this(), just like super() in the above statement ?

[This message has been edited by Umesh (edited March 29, 2000).]

maha anna
bartender
posted March 30, 2000 12:45 AM             
Umesh,
All constructors in any subclass will have the super() statement as their first statement by default.
Unless....
... when you put explicitly either this(..) or super(...) as first statement
of any subclass constructor, then the automatic insertion of super() statement is NOT DONE.
Since either ONLY ONE statement of this(...)
or super(..) can be as the first statement of any constructor.
Also note that the ansestor of all types in Jave, the Object class doesn't have a super class. Follow the foll. code.


class Base {
Base(){
}
Base(int i){
}
void printMe() {
System.out.println("BasePrint");
}
}

class Child extends Base {
Child(int i){//Ok ONLY super() inserted automatically
}
Child(double d){
super(); //Same as above. Since you inserted a super class constructor as 1st statement the compiler
//doesn't insert super() as in the above case
}
Child(float f){
this ((int)f);//Ok No super() inserted
}
Child(short s) {
super(10);//Ok No this() inserted
}

/*Child(byte b) {
this(10);
super(10);//Not OK either one of this(..) or super(..) can be present but NOT both
}
*/
/*Child(char c) {
System.out.println("maha");
super(10);//Not OK. super(..) or this(..) MUST be first statement in a constructor
}*/
/*void m1() {
super();//Not Ok super(..) this(..) can be present only in constructors
// NOT in methods.
}*/
void printMe() {
System.out.println("SubPrint");
super.printMe(); //OK because super.someMethod() is different from super(..) constructor invocation
}
static public void main(String[] a) {
}
}


Cut and paste this code as a Java appln and test the concepts.
I am writing this well past midnight. I might have missed some.
regds
maha anna

[This message has been edited by maha anna (edited March 30, 2000).]

sree
ranch hand
posted March 30, 2000 06:04 AM             
Thanks maha.

Umesh
ranch hand
posted March 30, 2000 08:24 PM             
Wonderful Anna.....its set in my mind.....thanls a lot.

|