aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding" Watch "overriding" New topic


Chris Armstrong

Joined: Jul 01, 2002
Posts: 4
Hi All,
Mughal says page 181,
"private methods in a class cannot be overridden"
class fruit {
private String getFruit(String name) {
return "apple";
class pears extends fruit {
String getFruit(String name) {
return "apple";
public class grapes {
public static void main(String args[]) {
pears p = new pears();
but in the above am able to compile the program even though a private method is being overriden ?
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
and now try this,

what happens?

[Blog] [Blogroll] [My Reviews] My Linked In
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Yes, your code did indeed compile. Even so, you did not override fruit.getFruit(). Here's an example to prove that fruit.getFruit() has not been overridden. An overriding method must have the same return type as the overriden method. Furthermore, the overridding method can not throw exceptions that are not thrown by the overridden method. If you change the return type of the pears.getFruit method or add some exceptions to the throws clause you will find that the code still compiles. Of course, you would also have to change the code that calls getFruit so that it accepts the new return type.

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="" target="_blank" rel="nofollow">Try my mock exam.</a>
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
from simon roberts
the overriding methods must not be less accessible than the method it overrides

in the above code..the private method is being overriden to be more accessible(i.e friendly). hence its legal to overide the method even though its private.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Simon Roberts is indeed correct. Section of the Java Language Specificationstates the following.
The access modifier of an overriding or hiding method must provide at least as much access as the overridden of hidden method, or a compile-time error occurs.

Even so, private methods are not overridden. Method overridding is a polymorphic device that allows the same name to be given to a method or variable at two or more levels of a class hierarchy. Polymorphism allows a subclass to replace a superclass where the subclass offers a compatible interface but possibly different behavior. A private method, however, is not a member of the interface of the superclass. A subclass that exposes a public method that has the same name as a private method of the superclass is not exposing a polymorphic replacement of the superclass method. Instead, the subclass is simply exposing a new method and polymorphism is not involved and method overridding has not occurred.
As I mentioned in my earlier post, try changing the return type of pears.getFruit() and of course change the line of code that calls pears.getFruit() in the grapes class. The fact that the pears.getFruit() method can have a return type that is different from fruit.getFruit() method is proof that method overridding has not occurred.
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
"private methods in a class cannot be overridden"
The above description is very wrong !
A superclass's private method can not be inherited so there should be no further consideration as with whether it can be overriden or not.
It should be changed to:
"There are no changes for a subclass to override
its superclass's private methods.The subclass can hide a superclass's private method in a way which looks like overriding."
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Alan Chong:
A superclass's private method can not be inherited so there should be no further consideration as with whether it can be overriden or not.

Yes, I agree. My earlier post was a long winded attempt to say the same thing.
[ July 05, 2002: Message edited by: Dan Chisholm ]
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9044
Just because a method in the subclass has the same signature as a method in the superclass does not mean it is overriding it. The subclass has no idea that the superclass even has a method with that signature, so its method is completely independent of the superclass's private method whether it has the same signature or not.
[ July 05, 2002: Message edited by: Marilyn deQueiroz ]

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
I agree. Here's the link:
subject: overriding