This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Could not get this!!

 
Karthik Subramanian
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello friends,
This following question is from Test-your Java Knowledge mock exam, Question # 4.
Determine the result of attempting to compile and run the following code:
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;
}
}
ANS: The code above prints 2
But i fail to understand how??
There is no overriding happening here.
How is the subclass able to call the private method of the base
class when the object type is of it's own(i,e subclass).
can somebody throw some light??
Thankx a million

 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthik,
Since Sub doesnot override the int g() method,
new Sub().g()
actually invokes the method defined in the superclass Tester. The g() defined in Tester now calls the private f() in the immediate class scope which is in accordance with Section 15.12.4 Runtime Evaluation of Method Invocation of the JLS.
I am sure this question has been discussed before, so if you do a quick search you will be able to find more descriptive answers.
Ajith

 
Karthik Subramanian
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello Ajith,
Thanks for the explanation.

But last night i did manage to get an answer for
this.
Before that, you said that the subclass object
is able to call private function from the superclass.Yes that is true.But i feel that there is more than just that.
Please correct me if i am wrong.
My understanding is that whenever a class inherits from another class it inherits all
methods declared in the superclass.
But if the method is marked private then the
subclass cannot see this method because it
is hidden.
In this case because we have declared the subclass object in the superclass the subclass
object can see and invoke the method which it had inherited.Hence it is able to invoke the
method f() which is declared in the superclass

I may be wrong.Kindly clarify
Thanks
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kartik,
Private methods are never inherited. They are not "hidden", but they are just not visible to the subclass. This is a very important concept to remember. Don't get confused between being inaccessible and being invisible.
The code here is a classic example of what I call "method encapsulation". It is just my coinage, and you will not get a definition for this term. Just as we encapsulate private data members providing them public accessor methods, we can encapsulate private methods, providing public accessor method to them!. In the code, the method g() is acting as a facade to communicate with the private method f().
Also, since g() is not private, it is accessible and inherited by the Sub class. Since the Sub class didnot override the g() method, new Sub().g() actually calls the method on the superclass, which happens to be our gateway to get to the private method f().
Hope it is all clear now.
Ajith
[This message has been edited by Ajith Kallambella (edited September 01, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic