This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock exam #1 Q59 explanation problem?!? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock exam #1 Q59 explanation problem?!?" Watch "Mock exam #1 Q59 explanation problem?!?" New topic
Author

Mock exam #1 Q59 explanation problem?!?

Ivan Ivanoff
Ranch Hand

Joined: Jan 04, 2002
Posts: 56
Hi all,
Please take a look at Mock exam #1 - question # 59.
I agree with answer option (4)
(Output of "Over.amethod"),
but I think that explanation is not 100% right:
Explanetion saying : "The names of parameters to an overridden method is not important."
But we are not dealing with any overriding here, because private method can not be overridden.
The method "private void amethod(int iBase)" only belongs to the Base class.
Please let me know what do you think ?
Thanks,
Ivan.

question # 59
What will happen when you attempt to compile and run the following code
class Base{
private void amethod(int iBase){
System.out.println("Base.amethod");
}
}
class Over extends Base{
public static void main(String argv[]){
Over o = new Over();
int iBase=0;
o.amethod(iBase);
}
public void amethod(int iOver){
System.out.println("Over.amethod");
}
}

1) Compile time error complaining that Base.amethod is private
2) Runtime error complaining that Base.amethod is private
3) Output of "Base.amethod"
4) Output of "Over.amethod"
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
You're right...
it would only make a difference if the declaration of o would be like:
Base o = new Over();
in which case the code wouldn't compile...
The explanation is not really accurate, I admit.
I guess it should say that overridding is not used in the code since o is an object of (compile-time) type Over and not Base
HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
Hi,
Private method cannot be overridden. Its partially correct. You can overide a private method in subclass to be more public. Now that method will act as new method, hidding the super class method. Hope it helps.
Further discussion you can find in below link
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014075


Arathi<br />Sun Certified Java Programmer
Ivan Ivanoff
Ranch Hand

Joined: Jan 04, 2002
Posts: 56
So it means that I CAN NOT override a private method !!!
- since private method only belongs to a supper class.
True/False ?
Some ppl saying true other false - what the truth for SCJP ?
Thanks,
Ivan
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
You cannot override private methods, that's right. But in the example you gave overridding is not used since the method amethod() is invoked on a object of type Over and not Base.
Basically, if you use overridding you cannot declare a method in a subclass that has the same signature as a private method in the superclass.
Again, if you change the code as I suggested you get the following error:

which clearly states that you cannot override a private method.
Now, a tricky thing is the following:
Say you a a private method in a superclass and you declare the same method in the subclass but you give it public access. Then, as long as you don;t use that method the code will compile and rune fine. But as soon as you try to invoke it you are gonna get a compiler error.
Does it make sense to you?
HIH
Ivan Ivanoff
Ranch Hand

Joined: Jan 04, 2002
Posts: 56
Valentin,
I'm 100% agree with you ! But not in one of Java books I could not find clear rule for that.
So here is a new rule that you can not find in any java 2 exam book :
A PRIVATE METHOD CAN NOT BE OVERRIDEN !!!
Everybody agree ?!? :roll:
Thanks,
Ivan Ivanoff
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Ivan,
just one thing, when you're in doubt the JLS RULES!!!
Check this out: 8.4.6 Inheritance, Overriding, and Hiding
You might find the definition you want in there. The JLS is the absolute Java resource for every Java developer. You should read it... Numerous of things in there
Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
Hi ,
I have question now. Usually methods are invoked by the type of object at runtime right. So why not Base o= new over(), will call over class method. I am getting confused. Will you explain it.
Ivan Ivanoff
Ranch Hand

Joined: Jan 04, 2002
Posts: 56
Agree for 200% !
JLS says :
A private method and all methods declared in a final class (�8.1.1.2) are implicitly final, because it is impossible to override them. It is permitted but not required for the declarations of such methods to redundantly include the final keyword.

Thank you all !
Ivan Ivanoff
Ivan Ivanoff
Ranch Hand

Joined: Jan 04, 2002
Posts: 56
Arathi Rajashekar ,
Because there is NO OVERRIDING in this case - you can not override a private method !
Ivan Ivanov.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
This is something that I will explain in an article that will be published in the February newsletter.
But here goes.
Declaration:
Base o = new Over();
The compile-time type of o is Base and the runtime type of o will be Over. The method to be invoked MUST be defined in class Over since the method will be invoked at runtime on o (which is of type Over). But since the compile-time type of o is Base, the latter MUST also declare the method. If that method (in Base) is declared private then we say that the invocation mode is non-virtual (which means in the JLS jargon that the method cannot be overridden by subclasses).
Now, if you declare the same method in Over (that is you override the method of Base) then the compiler is going to choke on it because overriding is not allowed.
The difference between
Over o = new Over();
and
Base o = new Over();
is that in the first case everything will be fine because the method amethod will be searched in Over both at compile and runtime, while in the second case, Base will be searched at compile-time and Over will be searched at runtime. The compile-time search will fail since the method amethod is private in Base.
I don't know if it clears things up... Let us know
Ivan Ivanoff
Ranch Hand

Joined: Jan 04, 2002
Posts: 56
Agree 100%.
Thank you Valentin !
Ivan Ivanoff
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Originally posted by Valentin Crettaz:
This is something that I will explain in an article that will be published in the February newsletter.
But here goes.
Declaration:
Base o = new Over();
The compile-time type of o is Base and the runtime type of o will be Over. The method to be invoked MUST be defined in class Over since the method will be invoked at runtime on o (which is of type Over). But since the compile-time type of o is Base, the latter MUST also declare the method. If that method (in Base) is declared private then we say that the invocation mode is non-virtual (which means in the JLS jargon that the method cannot be overridden by subclasses).
Now, if you declare the same method in Over (that is you override the method of Base) then the compiler is going to choke on it because overriding is not allowed.
The difference between
Over o = new Over();
and
Base o = new Over();
is that in the first case everything will be fine because the method amethod will be searched in Over both at compile and runtime, while in the second case, Base will be searched at compile-time and Over will be searched at runtime. The compile-time search will fail since the method amethod is private in Base.
I don't know if it clears things up... Let us know


Man! The best explanation of a somewhat confusing topic that I have read in all my time studying. Thanks!! (I've cut and pasted your explanation into my notes that I'm collecting while studying: )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock exam #1 Q59 explanation problem?!?
 
Similar Threads
doubt private method
JWhiz Question
Marcus G exam 1 -58,59
private method...
corrections in marcus green test1