Scenario
public class Parent {
int x = 5;
}
class Child extends Parent {
int x = 6;
}
class Demo {
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.x);
}
}
Which value will get printed - parent or child one ?
Explanation
Parent class variable will be printed because instance variables always bound at compile time, here compiler already decided to use variable of parent as declared type is Parent.
Result
5
No comments:
Post a Comment
Note: only a member of this blog may post a comment.