File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes override of private method 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 "override of private method" Watch "override of private method" New topic

override of private method

Vlad Golodov
Ranch Hand

Joined: Jul 05, 2004
Posts: 50
In whizlabs Exam Simulator I have found one question:

A private method may be overridden by a private, friendly, ____, or public method.

I needed to fill in the gap. Here could be only one right answer - protected, but, if i'm not mistaken, private methods ARE NOT overridable? Am I wrong?

SCJP, SCJWSD prepairing<br /><a href="" target="_blank" rel="nofollow"></a>
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Private methods are not inherited and, therefore, can not be overridden.

SCJP Tipline, etc.
Bobby Seitz

Joined: Oct 02, 2004
Posts: 4
The question on the mock exam is worded incorrectly so the other post is correct in saying that nothing could answer the question correctly. However, in case you are wondering the question is trying to say that while the private method cannot be overidden, because it is not inherrited, it can be redefined. So the correct statement would be that a private method can be redefined as a public, friendly (default), protected, or private method. However, for the real exam remember that this method is not actually overidden (despite how it looks), and polymorphism does not apply.

Hope this helps!

Vlad Golodov
Ranch Hand

Joined: Jul 05, 2004
Posts: 50
Exactly I'm on the same opinion.
Arnab karmakar
Ranch Hand

Joined: Oct 04, 2004
Posts: 46
Hi Vlad,

The following is the code which could explain u the concept that private methods could not be overriden.

class overridePrivateClass
int data;
private void func()
class derivedClass extends overridePrivateClass
void func()
class overridePrivate
public static void main(String [] args)
derivedClass ob = new derivedClass();

Here, overridePrivateClass has got a private method func.
From the code u might say that in the derived class " derivedClass"
the function func has been overriden, but actually the function is
not overriden in derivedClass. The func in overridePrivateClass is
not vissible in derivedClass, since it is declared private.
And when u enter in derivedClass , the is no definition for func and
so we could write our own code for that function.

I hope it could help u.

Vlad Golodov
Ranch Hand

Joined: Jul 05, 2004
Posts: 50
Thanks for you all!

Maybe this code will explain more clearly

public class PrivateMethodTest extends PrivateMethodClass{
public void method(){}
public static void main(String[] args){
PrivateMethodClass mClass = new PrivateMethodTest();

class PrivateMethodClass{
private void method(){}

Here is the almost the same situation as before, in my opinion. Private method is not visible outside.
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1844

private methods are not visible outside. so we cannot override.

Krishna Srinivasan
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
It is sorta covered in the JavaRanch Style Guide.
subject: override of private method
Similar Threads
What's the deal with private classes
Servlet that reload any class file in server
help with this code// constractors
statically bound & dynamically bound means??
Can a private method be overridden by a subclass ?