• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessibility

 
Kalpesh Pandya
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This Question was originally posted by Manish.
I don't know how to re-open already posted topic, so I am posting it again.
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
 
Kalpesh Pandya
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi manish,
If you remove private from the method f() than it will print 1 as output. Private methods cannot be overridden, as they are not visible to sub class. method f() in subclass has nothing to do with method f() in super class. So when g() of super class is calling f(), only method available in super class is f() with return 2.
Hope this will help you !
Correct me if I am wrong, because earlier explanation given by others to this question confused me a lot !
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kalpesh!
If you want to answer smbd question, you can hit blue button "Post Reply" at the top or at the bottom of the page.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic