• 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:

Overriding Private Method

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please refer to RHE pg79:
"A private method may be overridden by a private, friendly,
protected, or public method".
In my opinion since private methods are not accessible
by subclass, there is no question of overriding.
Meaning PRIVATE METHODS CAN NEVER BE OVERRIDDEN.
However, a subclass can declair the method with same
name, same return type and similar parameters but
that would be seperate subclass method and not the
oevrriding method.
Am I misunderstanding the RHE?
Regards
Sanjay
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RHE is wrong here. You cannnot override a private method. Private methods belong to the class only and are not eligible for inheritance - therefore cannot be overridden
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are we splitting hairs here? Can we say with any degree of certainty whether we are "overriding" a private method, or declaring a new one? for example consider:
class privatebase
{
private void amethod() {
System.out.println("In parent's amethod");
}
}
public class privatedemo extends privatebase
{
private void amethod() {
System.out.println("In child's amethod");
}
public static void main(String[] args)
{
privatedemo pd = new privatedemo();
pd.amethod();
}
}
This outputs: In child's amethod
Can we say for sure that the amethod() in privatebase was not overridden, and a new one was declared? And for that matter, does it make a difference?
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes we CAN say for sure that private methods CANNOT be overridden
Here's a detailed explanation :
With respect to INSTANCE methods here are the rules -
Rule 1 : ONLY non-private and accessible methods can be overridden.
Only non-private and accessible methods which are not over-ridden are inherited.
Note : Private methods CAN be accessed in a sub-class but they are NEVER inherited , for ex :
class Outer {
class Inner {
private void method() {}
}
class Sub extends Inner {
private void method() {
super.method(); // legal - private methods CAN be accessed in sub class
}
}
IT DOES make a lot of difference to say that private methods ARE NOT over-ridden, coz if they were, we would have dynamic binding on them.
More over, access specifiers would be more restrictive.
And the problem of "throws clause" incompatibilty would crop up.
Moreover, the return type would have to be same !!!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't like taggin on to old posts...

But...

Deepak provided code that claims a subclass can call its superclass's private methods... Is it just my JVM or is that wrong?

I actually compiled the code as provided, but I think it compiles because of the use of nested classes, not because a subclass is allowed to call superclass private methods.

To be sure, I changed the code so that the nested classes were no longer nested and the code fails to compile.

I am not just trying to be picky and say someone is wrong... I actually do want to know. I always thought that private access means that that class (not including subclasses) alone can see that member.

Clarification please?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bill Howerton,

In your code while instantiating privatedemo object refer it to privatebase class.

privatebase pd = new privatedemo(); //1
pd.amethod();//2

Line 2 will not compile saying that amethod has private access in privatebase, which means that you cannot override private methods.

thankyou,
Srinivas
SCJP1.4
[ January 28, 2006: Message edited by: Srinivas Tuta ]
 
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get this from a cut and past of Bill's code:



G.
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And to show more clearly about inner classes:



G.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

However, a subclass can declair the method with same
name, same return type and similar parameters but
that would be seperate subclass method and not the
oevrriding method.




I feel Sanjay is correct, plse run the below by uncommenting super.call()



does that make some sense ???
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic