java - Why when you call method of parent from a second inheritance is from first inheritance? -


i confused:

i run example , results 2.

i can't find explication:

  • so have f object. called x.fun(x) here , copy of f object sent fun method.

  • here have fun(d d) have copy of x in d object.

  • d.method() calling me d method return 1;

however result 2...what wrong?

import java.util.*; import java.lang.*; import java.io.*;   class d {int method() {return 1;}} class e extends d {     int method() {         return 2;     } }  class f extends e {     int fun(d d){         return d.method();     } }  /* name of class has "main" if class public. */ class test2 {     public static void main (string[] args) {         f x = new f();         system.out.println(x.fun(x));     }  } 

  • the d.method() method call executes method fits runtime type of d.

  • in case, runtime type of d f (since create in main instance of f , pass fun() method).

  • f extends e, , e overrides method(). therefore d.method() calls e's implementation of method(), returns 2.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -