Why this code is printing 20 20 instead of 20 10?
java
Solution
Short answer: you call the `callAge` as it is implemented in `A`. But that method calls `getAge` which is overriden (thus Java follows the implementation like it is done in `B`).
Longer version:
When you call:
classb.superesult();
It will invoke `super.callAge()`, so that means the `callAge()` of class `A` is executed. `callAge()` in his part invokes `getAge()` but since the object is an instance of class `B` and `getAge()` is overridden, it returns `20`.
Mind that if you call `super.foo()` you only call the super foo. `super` thus does not mean that you "alter the context": all the calls that result out of the super call are still resolved through a dynamic binding on the object (and the object is still an instance of `B`). All calls `foo` does can be overriden (except if these are marked `final`). This is in most cases the desired bahavior.
Problem
When I am calling the `getAge` and `superresult` methods, I am expecting 20 10 as output but the code is printing 20 20. Why do I get a different result? ``` package package1; public class MyClassA { protected int size; public MyClassA() { } protected int getAge() { return 10; } public int callAge() { return getAge(); } } package package2; import package1.MyClassA; public class MyClassB extends MyClassA { protected int getAge() { return 20; } private int superesult() { return super.callAge(); } public static void main(String args[]) { MyClassB classb = new MyClassB(); System.out.println(classb.getAge()); System.out.println(classb.superesult()); } } ```