This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can a private method be overridden by a subclass ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can a private method be overridden by a subclass ?" Watch "Can a private method be overridden by a subclass ?" New topic
Author

Can a private method be overridden by a subclass ?

Mehul Mehta
Greenhorn

Joined: Aug 22, 2006
Posts: 7
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

Joined: Jan 10, 2007
Posts: 122
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.
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
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

Joined: Jan 10, 2007
Posts: 122
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.
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
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

Joined: Nov 14, 2006
Posts: 22
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...


SCJA 86%<br />SCJP 5.0 98%<br />SCWCD 1.4 79%<br />SCBCD 5.0 83%<br />--------------------------------<br />"I drank what???" - Socrates
siva prasaad
Greenhorn

Joined: Jan 10, 2007
Posts: 23
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

Joined: Jan 10, 2007
Posts: 23
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

Joined: Jan 07, 2007
Posts: 5
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

Joined: Aug 22, 2006
Posts: 7
Thanks Anton Uwe for the clarification and some good clean examples.
Pratap koritala
Ranch Hand

Joined: Sep 27, 2006
Posts: 252
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

Joined: Jan 10, 2007
Posts: 122

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can a private method be overridden by a subclass ?
 
Similar Threads
Overide Question
Static and Instance Blocks..
Exam Objective confusion
abstract and private
Methods