Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding

 
Chris Armstrong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Mughal says page 181,
"private methods in a class cannot be overridden"
-----------------------------------------------
class fruit {
private String getFruit(String name) {
System.out.println(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();
System.out.println(p.getFruit(""));
}
}
---------------------------------------------
but in the above am able to compile the program even though a private method is being overriden ?
Armstrong
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and now try this,

what happens?
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris,
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
 
zarina mohammad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
zarina
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zarina,
Simon Roberts is indeed correct. Section 8.4.6.3 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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic