wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can private methods be overridden? 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 "can private methods be overridden?" Watch "can private methods be overridden?" New topic
Author

can private methods be overridden?

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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

The best way to figure it out is to try it yourself...



In this code, the method of the subclass is *not* called, hence, technically, it hasn't been overridden.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Marco Reuel Perez
Ranch Hand

Joined: Apr 16, 2009
Posts: 45
Priya,

private methods can be overriden !

Here is a way:
Which class can access the private members of a class? The class itself AND any of its inner classes !

So if there is an inner class within a class, and that inner class is the sub class of the outer class and if the outer class has a private method, then the inner class can override that method and broaden its access within the inner class !

Hope you got my point.

-Reuel


SCJP 6 - 91%
<><
armando fonseca
Ranch Hand

Joined: Apr 03, 2009
Posts: 49
Hi priya,

It might seems that you can override private methods, but what is really happening is not overridden. Remember that private methods are not inherited, therefore you are just writing a method in the subclass that happen to have the same name and signature of its super type. The reason that you can get away with your code, is that when you call the b.method(), is that there is private method in your current class( therefore you can access it) at compile time. Also, I if the two classes are in one file, I don't think your code will compile, because only one class can have the "public" access modifier.

Regards,
Armando


scjp6-90%
Marco Reuel Perez
Ranch Hand

Joined: Apr 16, 2009
Posts: 45


In this code, Inner inherits (??) the Outer's printMe method. Compiling and running the above method as is we get the output:


But if Inner does not extend Outer then super.printMe() doesn't work.
If we comment out lines 7-9 and line 12, we get the output
This is because every private member of the Outer is visible to Inner.

In a sense, when the Inner class extends the Outer and redefines Outer's private methods I assume it is overriding Outer's methods though they are private (??).

-Reuel
armando fonseca
Ranch Hand

Joined: Apr 03, 2009
Posts: 49
hey marco!
You are 100% right, and I really like your example about inheritance and inner classes. In this case , still is not overridden. Your example is more about a shadow method. Since you didn't inherited printMe from Outer, you will have no issue in right your own printMe in Inner, but because is an non-static inner class member, you still have access to the outer private methods. Your example is very good and tricky, but once again , is shadow method and not overridden.

-Armando
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



This is not overridden at all. You are assigning Inner() object to Inner reference type (overridden comes to picture when reference type and object type are different).

one thing that is possible in inner class and not in inherited classes ( in context to private methods) is accessing private method with super keyword
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Marco your example is good. But as everyone says, still there is no overriding. Just to test it try this program



I've made the printMe method final in Outer class still you can declare printMe in the inner class...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Marco Reuel Perez
Ranch Hand

Joined: Apr 16, 2009
Posts: 45
Ankit,

I liked your explanation. That's right a private method can never be overriden.

But only thing that makes it "pseudo-possible" is the ability to call super.somePrivateMethodInSuperClass() in a sub-class. Which looks like calling a super class' version of the overriden method. I agree though it looks like overriding it is just shadowing.

I was tending to think in the way the question was asked in the mock exam Priya mentioned. May be the question should have been asked differently. It is misleading..


Reuel.
Marco Reuel Perez
Ranch Hand

Joined: Apr 16, 2009
Posts: 45
Balu Sadhasivam wrote:

This is not overridden at all. You are assigning Inner() object to Inner reference type (overridden comes to picture when reference type and object type are different).


testMe() is not the overriden method.


one thing that is possible in inner class and not in inherited classes ( in context to private methods) is accessing private method with super keyword


No the inner class (which does not extend the outer class) cannot access the outer class' private methods using super keyword. Because if inner doesn't extend anything super would refer to Object.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Yes, there is no overriding.


The code above prints "I am Outer"

While there is no overriding, this is an interesting example, good job Marco.

The way I would explain this is to say that an inner class can access a private method of the enclosing class it extends, but the method doesn't become a part of itself. So when a method of the same name is declared in the inner class, it doesn't override but merely hides the method defined in the enclosing superclass (however, the method in the enclosing superclass should still be accessible using the enclosing instance (in this case Outer.this.)


All code in my posts, unless a source is explicitly mentioned, is my own.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



testMe() is not the overriden method.


I meant this line as both reference type and object type are same and no matter wat method called , its instance of Inner and type of Inner.



No the inner class (which does not extend the outer class) cannot access the outer class' private methods using super keyword. Because if inner doesn't extend anything super would refer to Object.


Yup agree.
Marvin Soliven
Greenhorn

Joined: Apr 24, 2009
Posts: 2
Ruben Soto wrote:Yes, there is no overriding.


and if we comment out the printMe() method from the Inner class, we can see that private methods cannot even be inherited, i.e., calling inn.printMe() instead of inn.testMe() in the main() method, will generate a compile error.

hence, there will be nothing to override
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Marvin Soliven wrote:
Ruben Soto wrote:Yes, there is no overriding.


and if we comment out the printMe() method from the Inner class, we can see that private methods cannot even be inherited, i.e., calling inn.printMe() instead of inn.testMe() in the main() method, will generate a compile error.

hence, there will be nothing to override

You are absolutely correct, Marvin. The outer class private methods (which are not inherited in Inner) are only accessible within Inner's code via either unqualified access or access through the Outer.this instance.

Welcome to the ranch, by the way.
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
Oh man...
it's very easy to say when it is overridden and when it's not.
just use eclipse! lol

just kidding. but sure eclipse and other ides can help. xD
Greg Messner
Greenhorn

Joined: Mar 11, 2009
Posts: 3
A simple way to verify that the Inner.printMe() is not overriding Outer.printMe() is to use the @Override annotation on Inner.printMe(), doing so will cause the compile to fail with the following message:

Outer.java:6: method does not override or implement a method from a supertype


If you leave off the @Override annotation it compiles just fine.
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1844

Private methods never can be overriden


Krishna Srinivasan
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
Greg Messner
Greenhorn

Joined: Mar 11, 2009
Posts: 3
Krishna Srinivasan wrote:Private methods never can be overriden

Most here know that private methods cannot be overridden, I was just demonstrating that fact by using the @Override annotation.

If you read above some seem to be confused by using an inner class and what was happening when that inner class was a sub class of the outer class which contained the private method.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Greg Messner wrote: I was just demonstrating that fact by using the @Override annotation.


Yup, that's a good approach, why didn't I think of it ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can private methods be overridden?
 
Similar Threads
can we override private method?
static method can't be overridden
Accessibility
Overriding Private Methods
Confused in function overriding