public class Tester { public static void main(String[] args) { System.out.println(new Sub().g()); } private int f() { return 2; } int g() { return f(); } } class Sub extends Tester { public int f() { return 1; } } I think output should be 1 but it is printing 2. Methods can be overridden to be more public...then also ??? Please correct me !!! Manish
vijay krishna
Greenhorn
Joined: Oct 17, 2000
Posts: 8
posted
0
Cool! In method int g() , it is return f() that is being called not Sub().return f()
Manish Singhal
Ranch Hand
Joined: Sep 21, 2000
Posts: 104
posted
0
Vijay I think there should be implicity "this". Manish
Bin Zhao
Ranch Hand
Joined: Oct 04, 2000
Posts: 73
posted
0
Why go() method invokes f() in Tester class not the f() in Sub class?
vijay krishna
Greenhorn
Joined: Oct 17, 2000
Posts: 8
posted
0
You are right manish. It has "this" , but see carefully this corresponds to one which is running! (i.e., Which has main method in it).
midrisi
Greenhorn
Joined: Oct 22, 2000
Posts: 7
posted
0
It will print the value only 2 When you run the code it Sub().g() will be called as the inheritance suggest.But the g() will not having any access to f() function of Sub() as it is a function of parent class.So it will call f() function of Tester class itself.
I think your doubt has been cleared. thanks ------------------
[This message has been edited by midrisi (edited October 22, 2000).]
Manish Singhal
Ranch Hand
Joined: Sep 21, 2000
Posts: 104
posted
0
Thanks Midrisi. Doubt removed upto some extent. Manish