File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessibility

 
Manish Singhal
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool!
In method int g() , it is return f() that is being called not Sub().return f()
 
Manish Singhal
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijay I think there should be implicity "this".
Manish
 
Bin Zhao
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why go() method invokes f() in Tester class not the f() in Sub class?
 
vijay krishna
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Midrisi. Doubt removed upto some extent.
Manish
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic