Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can a private method be overridden by a subclass ?

 
Mehul Mehta
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a private method be overridden by a subclass ? Ref: SCJP Study Guide 5 by K & B on page 31.

I tried the following code :-

// ONE. java

package testsubclasssinheritance;
public class One {
private void myMethod(){
System.out.println ("This is my private from method One");
}
}


// TWO.java
package testsubclasssinheritance;
public class Two extends One {

public void myMethod(){
System.out.println ("This is my private from method Two");
}

public static void main (String[] args){
Two t = new Two();
t.myMethod();
}
}


// RESULT
The result was the code compiled an printed out
"This is my private from method Two"

// QUESTION
Does that mean that the private method can be OVERRIDDEN ???
 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Class Two knows nothing about the private method of class One. And so it is no problem to define a new method with the same name (and return type). No overloading or overriding takes place, it's just a new method (unluckily) having the same name.
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes,A method can be Overridden with less Restrictive Access

As public,protected and default are less Restrictive than Private ,they can override the private method(of course,Private methods won't be inherited)
[ January 13, 2007: Message edited by: ramya sri ]
 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mhhh, let's take a look at this code:

If it is overriding, than we would get a compile error, because final methods can't be overridden. But there is no error at all.

Or look at this code:

If it would be overriding, we would have a compile error (overriding methots cannot throw "more" checked exceptions than the base method declared.
But here is no compile error,too.
Or look at this code:

If it would be overriding, we would get a compile error.

So, it's not overriding, we just have a new method which happen to have the same name.
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we able to invoke Subclass method with Super class Reference on the Runtime Object of Subclass

Super s=new Sub();
S.InvokeSubclassMethod();
with the signature of Private method
 
Dustin Johnson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although you reference the variable as a Super class, you are initializing it as a subClass... therefore you should only be able to use the private methods of the subClass. If that was what you were asking...
 
siva prasaad
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes , You can definitely do it.

As per the concept the private member of the class is visible to itself and not any other class or sub-class.

Hence the sub-class has no idea about the private method you defined in the SuperClass.

Since it doesn't know about the existence of same method in the Super Class, it can define the same method which can be treated as a normal method for the sub class.

So in your case waht the JVM sees is " a normal method of your sub-class" and executes successfully.

Is this information fine or if i miss anything please inform.
[ January 13, 2007: Message edited by: siva prasaad ]
 
siva prasaad
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Missed out in my prevoius post,

Importantly , We should be aware that, Here we are not at all using the Overrding Concept.

The above 2 posts are for the 1st question....please make a note
[ January 13, 2007: Message edited by: siva prasaad ]
 
Andrew Affolter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like to think about this in the sense that the sub class has no idea about the private member of the super class and can't overload something it doesn't know about. The subclass could create its own method that was the same but its still not really overloading it.
 
Mehul Mehta
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anton Uwe for the clarification and some good clean examples.
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes Anton Uwe,as you said,this code giving compiler error instead of printing 4,4 .




[ January 14, 2007: Message edited by: ramya sri ]
 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You want to call a private method of the superclass. This method is not visible in the subclass. So you will get a compile error. You don't "need" to have in the subclass: You will get exactly the same error without it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic