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 ofd
.in case, runtime type of
d
f
(since create inmain
instance off
, passfun()
method).f
extendse
, ,e
overridesmethod()
. therefored.method()
callse
's implementation ofmethod()
, returns 2.
Comments
Post a Comment