aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding private methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding private methods" Watch "Overriding private methods" New topic
Author

Overriding private methods

Ricardo Cortes
Ranch Hand

Joined: Jan 23, 2002
Posts: 140
I got this question from the Round-up Game. Can someone please help me clarify the answer?
===
#20: Can a private method of a superclass be declared within a subclass?
Answer:
Yes. Trick question! Private methods can NOT be overriden, but they CAN be re-declared/redefined in the subclass, and called directly on the subclass object. But polymorphism will not apply.
===
In my example code below, I have implemented the exact same method in my subclass. The compiler doesn't complain. Is this because the method is being redefined/redeclared in class TestSubclass?
public class Test {
private void privateMethod() {
System.out.println("Inside privateMethod()...");
}
}
class TestSubclass extends Test {
private void privateMethod() {
int i=0;
System.out.println("Here is i: "+i);
}
}
[ January 23, 2002: Message edited by: Ricardo Cortes ]

Sun Certified J2EE Architect for the J2EE Platform (Part 1)<br />Sun Certified Web Component Developer for the J2EE Platform<br />Sun Certified Programmer for the Java 2 Platform
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
You can redeclare a private method in a subclass without any problem, the only thing you cannot do is use polymorphism. Here is an example:

If you uncomment line 1 you'll get an error at compile time stating:
B.java:13: method() has private access in A
a.method(); // line 1: this won't work because you are using polymorphism
^
1 error
Anyway private methods declared in a superclass shouldn't be redeclared in subclasses. This is bad practice and confusing to people that will come after you on the project... Consider another method name instead...
HIH
[ January 23, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Ricardo Cortes
Ranch Hand

Joined: Jan 23, 2002
Posts: 140
Thank you Valentin. It took me forever to understand your example until I saw that sup and sub are instantiated from different classes and not the same class! Classic upcasting...
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I'm sorry I should have chosen better identifiers... In fact, I'll change that right now.
Superclass will be A and Subclass B
I'll remember that next time... Sorry again
[ January 23, 2002: Message edited by: Valentin Crettaz ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Another way to think about this issue is that private members are NEVER inherited by a subclass.
Since they aren't inherited, they don't "exist" in the subclass. Therefore, they can't be overridden. (You can't override something that's not there to begin with, right?! ) The most you can do is define a new method from scratch that happens to share the same name and signature as a private method in the subclass, but these two methods then have absolutely no realationship with each other.
Rob


Rob
SCJP 1.4
Ricardo Cortes
Ranch Hand

Joined: Jan 23, 2002
Posts: 140
No No. Don't worry about it. Your example would have made a very good exam question!
Originally posted by Valentin Crettaz:
I'm sorry I should have chosen better identifiers... In fact, I'll change that right now.
Superclass will be A and Subclass B
I'll remember that next time... Sorry again
[ January 23, 2002: Message edited by: Valentin Crettaz ]
Ricardo Cortes
Ranch Hand

Joined: Jan 23, 2002
Posts: 140
This is a very good explanation as well. I like thinking about the situation in the way you described Rob. Thanks.
Originally posted by Rob Ross:
Another way to think about this issue is that private members are NEVER inherited by a subclass.
Since they aren't inherited, they don't "exist" in the subclass. Therefore, they can't be overridden. (You can't override something that's not there to begin with, right?! ) The most you can do is define a new method from scratch that happens to share the same name and signature as a private method in the subclass, but these two methods then have absolutely no realationship with each other.
Rob
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610

No No. Don't worry about it. Your example would have made a very good exam question!

Yeah I'm thinking about writing one....
Just kidding
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
great explanation Rob and Valentin, the exam couldnt be that trickey? or could it?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
That sort of question could well happen, I wouldn't think that it couldn't if I were you
eric sun
Greenhorn

Joined: Jan 22, 2002
Posts: 5
Originally posted by Valentin Crettaz:

Yeah I'm thinking about writing one....
Just kidding

Yes, your example is a very good.
Regards
Eric


Nice to meet you here. I like Java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding private methods