File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
    Bookmark Topic Watch Topic
  • New Topic

can we override private method?

 
Ketuman Joshi
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 186
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
------------------
 
Sridevi Shrikanth
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
Tualha Khan
Ranch Hand
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
******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
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hey,
No body confirmed my answer above!!!
Please do!!!
BYe,
Tualha Khan
 
Peter Tran
Bartender
Posts: 783
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Tualha,
You are correct. Now are you happy?
-Peter
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
Tualha, to make you feel better I found an old thread with some code example demonstrating what happens when you "override" private method.
 
Golam Newaz
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic