I'm facing some difficulties while trying to understand, what actually happens when we initiate an instance of a child class.
public class A {
2. public int x, y;
3. public A () { x=1; y=2; }
4. public int getx () { return x; }
5. }
6. public class B extends A {
7. public int x, z;
8. public B () { super(); x=3; y=4; z=5; }
9. public int getx () { return x; }
10. public int getz () { return z; }
11. }
12. public class Prob1 {
13. public static void main (String[] args){
14. A o1 = new A();
15. A o2 = new B();
16. B o3 = new B();
17. System.out.println(o1.x);
18. System.out.println(o1.getx());
19. System.out.println(o1.y);
20. System.out.println(o1.getz());
21. System.out.println(o2.x);
22. System.out.println(o2.getx());
23. System.out.println(o2.y);
I would love to here a detailed explanation of what is going on here exactly, but the main thing I can't understand is why line '21' prints the number 1, and line '23' prints the number 4.