• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interesting mock question

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this question interesting and thought of sharing with you all. I will post the answer later unless somebody post.
public class A{
private int f(){
return 2;
}
int g(){
return f();
}
public static void main(String[] args){
System.out.println( new B().g());
}
}
class B extends A {
public int f(){
return 1;
}
}
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It prints 2. Because f() is private in Class A.
------------
Sainudheen
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
I am having a hard time figuring how the answer is 2. Can somebody put some light on it.
Thanks
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A private method is not inherited and hence it cannot be overridden. As a result the method g() in class A will always call f() in A.
If f() were declared with package access ,protected access or public access in class A then the f() in B would have over-ridden the f() in A and hence at run-time f() in B would have been called.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
private methods are implicitly final, so the compiler early binds them, maybe even inlines them. The subclass is free to redefine the method, but the two methods have nothing to do with each other. The subclass doesn't know that super's private version exists, and super doesn't care about sub's version - it will always use its own.
 
Asha Sat
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I completely agree with Steve's view, it is more appropriate and easier to understnd the private method logic if you think it is private final and complier would do something special on it.
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic