jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Could not get this!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Could not get this!!" Watch "Could not get this!!" New topic

Could not get this!!

Karthik Subramanian

Joined: Aug 23, 2000
Posts: 27

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

Joined: Mar 17, 2000
Posts: 5782
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.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Karthik Subramanian

Joined: Aug 23, 2000
Posts: 27

Hello Ajith,
Thanks for the explanation.

But last night i did manage to get an answer for
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
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
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.
[This message has been edited by Ajith Kallambella (edited September 01, 2000).]
I agree. Here's the link: http://aspose.com/file-tools
subject: Could not get this!!
It's not a secret anymore!