Originally posted by Edwin Wong:
Dear all
The followinf is the mock exam on http://www.geocities.com/sahirshah/applets/mocktest.html
I don't understand the result
Here is the code for question 1
........................
class Base{
int value = 0;
Base(){
addValue();
}
void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();
}
void addValue(){
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
................................
Question 1 answer is 40
Could any one can tell me why the parent woulc call the child method rahter then it's own method ?
Question 2 is almost the same but method and variable changed to static
....................................
class Base{
int value = 0;
Base(){
addValue();
}
void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();
}
void addValue(){
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
.................................................
The answer is 30
Anyone can help me ?
I am confuse about this
Thanks
Originally posted by Edwin Wong:
Thanks Lam
In order to clear my confusion, I want to ask one more question
Is that mean Polymorphism(runtime binding) won't work when the method is static ?
Just want to have a more clear picture
Thanks
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |