aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Accessibility Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Accessibility" Watch "Accessibility" New topic
Author

Accessibility

Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104
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
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
Vijay I think there should be implicity "this".
Manish
Bin Zhao
Ranch Hand

Joined: Oct 04, 2000
Posts: 73
Why go() method invokes f() in Tester class not the f() in Sub class?
vijay krishna
Greenhorn

Joined: Oct 17, 2000
Posts: 8
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
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
Thanks Midrisi. Doubt removed upto some extent.
Manish
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Accessibility