my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question  # 4 - Absolute java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question  # 4 - Absolute java " Watch "Question  # 4 - Absolute java " New topic
Author

Question # 4 - Absolute java

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Please go through 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;
}
}
Q: What will be printed in line 3??
According to me, answer should be 1. But the answer given is 2.
My Understanding:
When a new Sub() is constructed and the method g() is called, and method f() is in turn called from g(), I thought that f() in the Sub class is called( thereby printing 1) because java always goes up in the class hierarchy in finding the appropriate methods. Since it's getting a method f() in Sub itself, it'll call f() in Sub.
But when I refere to the explanation given in the answers, they say that f() in Sub not over-riding f() in Tester because f()in Tester is declared Private and hence cannot be Overridden.. Is this true?? Can't we over-ride a method which is declared private??
If you want the URL to above test, it's www.absolutejava.com/articles/test-your-java-knowledge.html
Thanks
Prabhu.
PS: Number of edits because of trying to create a perfect URL Link.. Please bear with me

[This message has been edited by Prabhu (edited May 03, 2000).]
[This message has been edited by Prabhu (edited May 03, 2000).]
[This message has been edited by Prabhu (edited May 03, 2000).]
[This message has been edited by Prabhu (edited May 03, 2000).]
Paul Wetzel
Ranch Hand

Joined: Nov 02, 1999
Posts: 107
if it is private you do not have access to it in the sub class (you cannot call it from the sub class) so it cannot be overridden.
[This message has been edited by Paul Wetzel (edited May 03, 2000).]
sunny
Greenhorn

Joined: Feb 01, 2001
Posts: 23
Yes, private method cannot be overridden. f() in Tester class is private and is not visible from outside (not even to it's subclass). So when you create a method f() with the same signature in subclass, you are not overridding f(), but you are defining subclass's own method.
Hope this helps -
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks for the info.
But, why is f() from Tester getting executed rather than f() in Sub?? Becuase we've created an instance of Sub and calling g() from there, is f() from Sub not supposed to execute??
Prabhu.
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Consider the fact that although we can not override the private f() method, we are still able to inherit it , since we are able to subclass the class in which it is contained. Having inherited it in this way, a call to f() refers to two methods in our subclass, but since the override was unseccessful, the super class's method regns.Indeed when f() is public, the override succeeds and we output 1.
Is this right ?
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Prabhu,
This is the rule.
All private methods are statically bound. Which means the left hand var's Class type is used. For example

LeftHandClassType varname = new SomeClass();
varname.somePrivateMethod()

In the above code sample, when you invoke somePrivateMethod(), it is the VERSION DEFINED IN LEFTHANDCLASSTYPE . All the private methods bindind are decided by compiler at the compile time itself. Simillarly all the member var and class var linkage. They also statically bound. Simillarly all static methods. They also statically bound.
ONLY instance methods are dynamically bound according to the real physical object created in the right hand side the above statements
regds
maha anna
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Maha,
Thanks for the infoo and BTW congrats on your score.
I'm sorry to say that I'm still confused with the above question. Can you please explain with reference to the code which I posted above??
I really appreciate your help.
Thanks
Prabhu.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Prabhu,
I am sorry I didn't answer your qstn with respect to what you wanted. Treat the above post as an add-on info and it is valid info. Now let me be specific to your code.
<pre>

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;
}
}

</pre>
It is like this. Read this carefully. Assume there is a Base class Base. This 'Base' class has a private method void basePrivateMethod() and a non-private instance method void baseInstanceMethod(). If this public instance method calls this base class's basePrivateMethod() means, IT REALLY CALLS ONLY THIS VERSION OF PRIVATE METHOD DEFINED in the SAME CLASS as the baseInstanceMethod() AT ALL TIMES , whenever this version of baseInstanceMethod() from the Base class is called. There is NO RELAXATION.
Assume there is a subclass which extends this Base class and defiens a new method which has SAME NAME/SAME PARAMETER LIST and everything else SAME as the private method in the Base class. This DOES NOT means this sub class is overriding this base class's private method. This is totally new method. Ok. Having said that, When you create an object of the subclass, and as you know this subclass inherits the non-private baseInstanceMethod() , and when you invoke the baseInstanceMethod() on the subclass object, what's is happening is the inherited method is called, which in turn invokes the private method of its OWN class where this baseInstanceMethod() is defined.
It is like this. If any method invokes a private var/private method, it is one which is in the SAME CLASS as the method is defined.
So having said that, In your code what is happening is Tester (in our case the Base class )defines a private method f() and a non-private method g(). There is a subclass Sub which extends this Tester (Base class). This Sub class inherits the base class non-private method g() ONLY. The private method f() in Base class (Tester) in NOT AT ALL known to Sub class. Now what you are doing is , you are creating a Sub class object by new Sub(). And then what you are doing is kind of cheating/peeking into the private stuff (method f()) of the Base, by calling the accessible method g(). Since the non-private method g() HAS FULL ACCESS to the private f(), because g() and f() are MEMBERS of the SAME class Base and eventhough you write a new method f() in the subclass which has SAME signature of the Base class, In effect what you are doing is , since you are calling the inherited version g() from the Base class, and if at all this inherited version calls any private stuff, it WILL BE FROM THE SAME CLASS where g() is defined which here is the Base class. So it returns 2
So when you do new Subclass().g(),
1. you create a subclass object
2. call the inherited g() from base class
3. this g() in turn calls its OWN private f() as in the SAME CLASS from WHERE it comes from
4. So the version of base class f() is called.
6. So value 2 is returned.
5. This is the essence of the all the above info.
Now let Maha ask Prabhu another related qstn.
Assume, the inherited g() from base class (Tester) is in fact OVERRIDDEN by the Sub class. In other words the Sub class re-defiens this g() from base class. Now what will happen?. Don't continue to read. Stop and think. Maha gives the answer right below. Assume Maha changes Prabhu's code as follows.
<pre>
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;
}
int g() {
return f();
}

}
</pre>
ANSWER for Maha's qstn:
-----------------------
Now Since the Subclass overrides the g() from base, and when you create a subclass object and call g() like foll
new Sub().g() now what happens, you are calling the overriden version of g() in subclass, so now if g() calls f(), which version will be called? It is the version fron the Sub class if the subclass overrides the f() from base, or else if it doesn't override f(), the inherited one from the base will be called. Since in our case the Sub class overrides f() the subclass version will be called. So it will return 1
Are you better than Maha's previous post, or now I made worse?
Please don't hesitate to ask again if any even small thing to be clarified. Let us fight till we get this concept right. I try to write some more sample program for you. I would also suggest you to play with this code by changing the modifiers private/overriding/notoverriding etc and convince yourself.
regds
maha anna.

[This message has been edited by maha anna (edited May 07, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks for the time and effort you've put to make things clear for me.
Most (Shall I say 90% ?) is clear from your explanation. But in the second question which u asked me, u said that u're overriding the method g() in sub class. Then when u instantiate the sub class and call g() (like new sub.g()), u explained that since sub is "OVERRIDING" the method f() from base, this f() in the sub class is called.
But to my understanding (from the above posts from others), I see that a private method cannot be 'OVERRIDDEN'?? So what are we actually doing when we have a f() in the sub class? Is it not a completely new method, not related to f() in the base class??
Meanwhile, I'll try to follow 'Maha' and play with the code.
Thanks
Prabhu.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
See, You got it Prabhu. I intentially wrote override in the previous post to test you. (Believe me ) That's why I wrote a note asking even if you have any small clarification come back like that.
We did it.
regds
maha anna
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Aha....
u dude.
Thanks
Prabhu.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question # 4 - Absolute java