wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confused help me!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confused help me!!" Watch "Confused help me!!" New topic
Author

Confused help me!!

sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Hi Guys,
I am confused with the follwing code.

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
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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.
<pre>
Child(int i){
test ();
}
</pre>
which is same as

<pre>
Child(int i){
super();
test ();
}
</pre>

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
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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.
<pre>
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) {
}
}
</pre>
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).]
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Thanks maha.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Wonderful Anna.....its set in my mind.....thanls a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused help me!!
 
Similar Threads
super in constructors
Another mock exam question.
Question regarding Polymorphism.
java programmer certificate
Jargon mock exam