File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can we override private method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic
Author

can we override private method?

Ketuman Joshi
Greenhorn

Joined: Dec 19, 2000
Posts: 9
Hi Guys,
can anyone tell me that whether we can override privat methods
for ex.
class {
private void method1(){
}

subclass {
private void method1() { // can we do this?
}
protected void method1 { // or can we do this
}
i would really appreciate if someone can give me response.
Thanks
Ketuman
Dominic Steng�rd
Ranch Hand

Joined: Feb 05, 2001
Posts: 186
Hello Ketuman!
You are allowed to override a method as long as the overiding methods access modifier is not more restrictive than the original one.
This means that it is ok to do the following:
class Parent {
public void aMethod() {
}
}
class Child extends Parent {
public void aMethod() {
}
}
But you are Not allowed to do the following:
class Parent {
public void aMethod() {
}
}
class Child extends Parent {
protected void aMethod() {
}
}
Regards
------------------
Dominic Steng�rd
Sun Certified Java 2 Programmer
------------------


Dominic Steng�rd<br />Sun Certified Java 2 Programmer
Sridevi Shrikanth
Ranch Hand

Joined: Jan 11, 2001
Posts: 31
Yes, private methods can be overridden.

Methods cannot be overridden to be more private. Hence the two options are possible.
private methods are not visible outside the members of the class, there will not be any point in overriding private methods with another private method.
qionghua yang
Ranch Hand

Joined: Oct 31, 2000
Posts: 68
Yes, the private method can be overriden in the subclass.
But the methods in super class can't be overridden with weaker access privileges. This means if you override a public method to be a private or protected method in the subclass, it will give you the compile error.
qionghua
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
Private methods cannot overriden in any way as they are not accessible. In case if in a sub class you define a method having exact signature as that in the super class, then, you are not overriding the super class method, you are declaring a new method in sub class. Try one thing, declare the super class private method as final, then try to override it in sub class. If it compiles, then it means that the super class method is not overriden, as final prevents overriding.
Hope it helps
Bye,
Tualha Khan

SCJP2, BEA WLS 6.0, DB2 UDB 7.1
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
******DISCARD THE ABOVE MESSAGE SEE HERE. A MINOR CHANGE******

Private methods cannot overriden in any way as they are not accessible. In case if in a sub class you define a private method having exact signature as that in the super class (which is also private), then, you are not overriding the super class method, you are declaring a new method in sub class.
To prove this point, try one thing, declare the super class private method as final, then try to override it in sub class. If it compiles, then it means that the super class method is not overriden, as final prevents overriding. Hence the sub class has declared it's own new method having the exact signature as the super class.

Hope it helps
Bye,
Tualha Khan

Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
Hey,
No body confirmed my answer above!!!
Please do!!!
BYe,
Tualha Khan
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Tualha,
You are correct. Now are you happy?
-Peter
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Tualha, to make you feel better I found an old thread with some code example demonstrating what happens when you "override" private method.


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Golam Newaz
Ranch Hand

Joined: Jan 08, 2001
Posts: 64
Hi,
Don't be confused about private access modifier and whether it can be overridden anymore until you read the followings. I got it from sun site. Actually, private method can not be overridden by method of subclass or by other type of object. Only to the class in which it is defined. But one class object can access the private members of another same class object. Kindly read carefully the followings and see the example:
Private access modifier:
The most restrictive access level is private. A private member is accessible only to the class in which it is defined. Use this access to declare members that should only be used by the class. This includes variables that contain information that if accessed by an outsider could put the object in an inconsistent state, or methods that, if invoked by an outsider, could jeopardize the state of the object or the program in which it's running. Private members are like secrets you never tell anybody.
To declare a private member, use the private keyword in its declaration. The following class contains one private member variable and one private method:
class Alpha {
private int iamprivate;
private void privateMethod() {
System.out.println("privateMethod");
}
}
Objects of type Alpha can inspect or modify the iamprivate variable and can invoke privateMethod, but objects of other types cannot. For example, the Beta class defined here:
class Beta {
void accessMethod() {
Alpha a = new Alpha();
a.iamprivate = 10; // illegal
a.privateMethod(); // illegal
}
}
cannot access the iamprivate variable or invoke privateMethod on an object of type Alpha because Beta is not of type Alpha.
When one of your classes is attempting to access a member varible to which it does not have access, the compiler prints an error message similar to the following and refuses to compile your program:
Beta.java:9: Variable iamprivate in class Alpha not
accessible from class Beta.
a.iamprivate = 10; // illegal
^
1 error
Also, if your program is attempting to access a method to which it does not have access, you will see a compiler error like this:
Beta.java:12: No method matching privateMethod()
found in class Alpha.
a.privateMethod(); // illegal
1 error
New Java programmers might ask if one Alpha object can access the private members of another Alpha object. This is illustrated by the following example. Suppose the Alpha class contained an instance method that compared the current Alpha object (this) to another object based on their iamprivate variables:
class Alpha {
private int iamprivate;
boolean isEqualTo(Alpha anotherAlpha) {
if (this.iamprivate == anotherAlpha.iamprivate)
return true;
else
return false;
}
}
This is perfectly legal. Objects of the same type have access to one another's private members. This is because access restrictions apply at the class or type level (all instances of a class) rather than at the object level (this particular instance of a class).
Hope it should clear it out,
Golam Newaz
------------------
priya thomas
Greenhorn

Joined: Mar 17, 2009
Posts: 15
I have a question from a mock exam:

Can a private method be overridden by a protected method:

Options1: True, Option 2: False

The correct answer as per the exam is Option1.

I am assuming the reason is: private methods CAN be overridden by protected methods, given that both methods are in the same class. Is that correct?

If it is a case of super class and sub class and the super class has the private method and the subclass has the same method with protected (or any) modifier, then the code will compile but we cannot say the method is overridden. Am i right?

Thanks
Priya
Ps: I read the previous posts, but didnt get a clear answer, so im posting this
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Priya, you should probably start another thread pointing to this one, as this one is too old, and there is a policy in the forum not to wake the zombies.

Thanks.


All code in my posts, unless a source is explicitly mentioned, is my own.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

Ruben Soto wrote:Priya, you should probably start another thread pointing to this one, as this one is too old, and there is a policy in the forum not to wake the zombies.

Thanks.


Agreed. Waking up a eight year old topic isn't very useful, as many, if not all of the users have probably moved on.

Anyway, I will be locking this topic -- but here is a link to the followup topic, in case anyone is interested.

http://www.coderanch.com/t/441483/Programmer-Certification-SCJP/certification/private-methods-overridden

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can we override private method?