3
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());
    }
}

when i am calling getAge and superresult methods i am expecting 20 10 as output but code is printing 20 20. Thanks in advance.

Phoenix Sagar
  • 43
  • 1
  • 4
  • 4
    Because `getAge` is overriden. – Oliver Charlesworth May 09 '17 at 11:19
  • `superresult` calls `callAge` in `MyClassA`, and `callAge` calls the overridden method `getAge`. The override in `MyClassB` returns 20. That's what overriding does. – khelwood May 09 '17 at 11:21
  • "i am expecting 20 10" why do you expect such results? – Pshemo May 09 '17 at 11:21
  • 1
    @Pshemo: I think the OP has in mind that if you call something through `super` you "alter the context" in the sense that all calls are now in the "super domain". – Willem Van Onsem May 09 '17 at 11:24
  • The concept to understand here is called **polymorphism**. In the sense of: there isn't much sense in asking "why" are things this way; more like: that is how polymorphism manifests itself in Java. So you have to learn how that concept works; and then you will understand the behavior you are observing. – GhostCat May 09 '17 at 11:29

3 Answers3

5

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.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
3

It's polymorphism. You have ovveridden in getAge() method. So when you call the method, the ovverdien method executes always.

The Java virtual machine (JVM) calls the appropriate method for the object that is referred to in each variable. It does not call the method that is defined by the variable's type. This behavior is referred to as virtual method invocation and demonstrates an aspect of the important polymorphism features in the Java language.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

Its because you have override getAge() method in child class. Now as MyClassB classb = new MyClassB(); is pointing to child class, so its method will be called instead of parent method

Afridi
  • 6,753
  • 2
  • 18
  • 27