• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Mock exam #1 Q59 explanation problem?!?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Private method cannot be overridden. Its partially correct. You can overide a private method in subclass to be more coderanch. 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
 
Ivan Ivanoff
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arathi Rajashekar ,
Because there is NO OVERRIDING in this case - you can not override a private method !
Ivan Ivanov.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree 100%.
Thank you Valentin !
Ivan Ivanoff
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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: )
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic