Originally posted by Stevie Kaligis:
The approach is using order of initialization :
1. The storage allocated for the object is initialized to binary zero before anything else happens.
2. The Process-class constructors are called. At this point, the overridden methodA( ) method is called, which discovers b value of zero, due to step 1.
3. Member initializers are called in the order of declaration, b value become 126,
4. The body of the Processor-class constructor is called, and found the value of b is 126, due to step 3.
hope that helps
stevie
Originally posted by Tracy Qi:
I am confused by this question for a long time too. Then I figure out 2 points here:
1. Varaible are shadowed not over loaded.
Originally posted by Richard Huang:
If I add a line in the program...
<code>
class Process {
byte b=127;
Process() {
System.out.println("Value of b is = " + b );
this.methodA();
}
void methodA() {
System.out.println("Value of b is = " + b );
}
public static void main(String [] args) {
Processor p = new Processor();
}
}
class Processor extends Process {
byte b=126;
Processor() {
System.out.println("Value of b = " + b);
}
void methodA() {
System.out.println("Value of b = " + this.b);
}
}
</code>
Answer:
value of b=127
value of b=0
value of b =126
according to you guys' explanation, the first b should be 0. Please tell me why. Thanks.
Originally posted by Tracy Qi:
I am confused by this question for a long time too. Then I figure out 2 points here:
1. Varaible are shadowed not over loaded.
2. subclass 's instance are instanitialized after call superclass's default constructor.
Correct me if i am worng.
1. According to me the methodA() of super class Process should be called.
Even if we do not use this , still the method of Processor class is called.
2. How can we call the methodA() of Process class from it's constructor.
By Vijay...
Late binding takes place when we assign a sub class object to super class reference object. During the run time it's decided to which sub class object the super class reference is pointing to. On the basis of that appropriate calls to the method are made. This would have taken place in this example if in the main method we had
Process p = new Processor();
where Process is the super class and Processor is the sub class.
But in this example it's like this
Processor p = new Processor();
the dynamically bound call is resolved at run-time because the object cannot know whether it belongs to the class
that the method is in or some class derived from it. For Consistency, you might think this is what should happen inside constructors.
This is not exactly the case. If you call a dynamically bound method inside a constructor, the overridden definition for that method is used. However, the effect can be rather unexpected, and can conceal some difficult-to-find bugs.
By Vijay...
Then what is happening when we make a call in Process class constructor
this.methodA();
What this 'this' will do ?
//**//
Secondly if we have 2 variables for example int i = 100; in the super class and int i = 200; in the subclass and if we want to print the value of i from super class then i = 100 will
get printed , even though in the main method if we have
made the object like this
Processor p = new Processor();
See the code discussed in the previous posts.
Consider Paul's rocket mass heater. |