Y
Yakov
The code below prints Good Bye and false. Why it does not print Mary and true?
(If you change the var declaration to B obj = new B() - it works fine. )
class A {
public boolean b_flag=false;
public String str_Flag="Hello";
}
class B extends A {
public boolean b_flag=false;
public String str_Flag="Good Bye";
public String toString(){
return "B.toString() String:" + str_Flag + "B: boolean: " + b_flag;
}
}
public class VariableOverridingTest {
public static void main(String[] args) {
A obj = new B(); // Prints Good Bye and false. Why?
// B obj = new B(); // Prints Mary and true
obj.str_Flag="Mary";
obj.b_flag=true;
System.out.println(obj);
}
}
(If you change the var declaration to B obj = new B() - it works fine. )
class A {
public boolean b_flag=false;
public String str_Flag="Hello";
}
class B extends A {
public boolean b_flag=false;
public String str_Flag="Good Bye";
public String toString(){
return "B.toString() String:" + str_Flag + "B: boolean: " + b_flag;
}
}
public class VariableOverridingTest {
public static void main(String[] args) {
A obj = new B(); // Prints Good Bye and false. Why?
// B obj = new B(); // Prints Mary and true
obj.str_Flag="Mary";
obj.b_flag=true;
System.out.println(obj);
}
}